I have a WCF service that accesses the database and adds data to 开发者_如何学编程it. now, I want to do some integration and/or system tests (automated).
How do I do it ?
I have to access the database, load initial data, call the service and then verify whether the data expected was actually loaded in the table.
is there any strategy for doing that you would recommend ?
I'm using WCF, Entity Framework, SQL Server, MSTests.
If you don't mind your test project taking a dependency on Entity Framework then here's how I would approach this.
- In the test project, create an EF model from the database, using only the table(s) you expect to be testing.
- Add a service reference to the WCF service from the test project.
- Create a test in the test project. In this test, use the EF context to create your initial test data.
- Call your service from the test.
- Check the database for the appropriate data, call your Assert(s).
- Clean up the database.
You should also consider using a lightweight file-based/in-memory database such as SQLite for this task. One simple way of achieving this would be to use EF to generate a model from your DB, then use the Update Database From Model tool to generate the SQL that will create the appropriate tables and constraints in your SQLite instance. This means no risk of munting the data in your main/dev DB and potentially faster tests.
Also regarding steps 3 and 6, some people would advocate using the setup/clean-up/tear-down affordance built in to MSTest. I generally don't start using these until I can see them helping with reducing code duplication, because in my opinion they make tests less clear and readable, but that's a personal thing.
精彩评论