简体   繁体   中英

wcf service testing with fake data

I have been looking for solution for a few days already and could find anything that would help to solve my problem. I have a WCF service for which I have to make some unit tests. The problem is that service takes data from database in this way:

using (var context = new MyProjectEntities())
{
  //here goes the actions
}

MyProjectEntities is autogenerated from edmx model i guess.. (Database first) So this way it takes all the data from the database and operates on it. My question is: whats the correct way to feed service with fake data for testing, instead of data from database?

The most trivial way is to use a live database. This isn't too flexible, because you need a new database in a fixed initial state for every single run, and also multiple developers can't use the same database at the same time.

What we do at my company is this: use a single-file database, namely SQL Server CE. If your code is database-engine independent, this can totally work, just change your connection string and you can even put your database to a fixed state by copying a template datafile to the right place. This isn't a really isolated unit test, but it's very simple to implement, it doesn't have the above problems, and you basically get what you need in the end. If your code is database-engine dependent, now you have one more reason to use an ORM solution like NHibernate or Entity Framework.

The best, most flexible, and also most complex solution is using a dependency injection or mocking framework. This is textbook stuff, there's tons of literature on that, it will give you all the flexibility there is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM