我有一个使用MVVM架构的WPF程序,它通过WCF访问SQL Server。我已经到了需要进行集成测试的程度,即程序运行正常,单元测试全部通过。
我发现很少有关于如何进行集成测试的信息,而且过去从未这样做过。我面临的问题是我需要访问WCF服务的所有View模型都在IDataService
的构造函数上有一个参数,它由我的View Model Locator注入到构造函数中。
以下是CdaService
引用我的开发数据库的服务示例:
public class DataService : CdaServiceManager.IDataService
{
public void Select(Action<CdaServiceManager.CdaService.DatabaseTable> callback, CdaServiceManager.CdaService.DatabaseTable thisTable)
{
using (CdaService.Service1Client webService = new CdaService.Service1Client())
{
var item = webService.Select(thisTable);
callback(item);
}
}
}
我在单独的服务器上创建了我的开发环境的精确副本,数据库和WCF服务完全相同。在集成测试期间,数据库将被清除并重置为以新数据开始。
在我的测试中,我有一个名为CdaService
的不同服务引用指向测试服务器WCF。当我调用我的视图mdoel构造函数时,我显然无法发送IDataService
的开发版本,因为它指向真实的服务器。
我看到为测试环境创建自己的IDataService
实现的一个选项,但是每当生产环境服务发生变化时,我都必须确保更改测试环境服务。似乎很草率。
有没有更好的方法来做到这一点。理想情况下,我可以向DataService发送我希望它使用的WCF引用,但我似乎无法绕过它。任何其他建议或想法将不胜感激。
答案 0 :(得分:0)
您是否考虑使用Moq或FakeItEasy等模拟库?
或者,如果您设置了测试Web服务,则不需要2个不同的服务引用,您只需在构建时将端点传递给客户端。例如:
var client = new YourServiceClient("Binding_in_your_config_file", "http://testservice.svc");
答案 1 :(得分:0)
回复这个问题真的很晚,但我们最终做的是使用预处理器指令来设置端点地址,然后在创建客户端时使用它。我们必须这样做,因为我们使用的是MVVM框架,所以DataService没有得到任何构造函数的实例化,它使用的是IOC容器。希望它可以帮到某人。
#if Test
public EndpointAddress address = new EndpointAddress("http://localhost/xxx/Service1.svc");
#else
public EndpointAddress address = new EndpointAddress("http://xxx.azurewebsites.net/Service1.svc");
#endif
public void Select(Action<CdaServiceManager.CdaService.DatabaseTable> callback, CdaServiceManager.CdaService.DatabaseTable thisTable)
{
using (CdaService.Service1Client webService = new CdaService.Service1Client("WSHttpBinding_IService1", address))
{
var item = webService.Select(thisTable);
callback(item);
}
}