简体   繁体   English

用假数据进行WCF服务测试

[英]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. 我有一个WCF服务,对此我必须进行一些单元测试。 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. 我猜MyProjectEntities是从edmx模型自动生成的。(首先是数据库)因此,它从数据库中获取所有数据并对其进行操作。 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. 我们在公司的工作是:使用单文件数据库,即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. 如果您的代码是数据库引擎相关的,那么现在您还有另一个理由使用诸如NHibernate或Entity Framework之类的ORM解决方案。

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. 这是教科书,其中有大量的文献资料,它将为您提供所有的灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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