【发布时间】:2013-03-26 12:16:09
【问题描述】:
我有一个类使用大致如下所示的 Web 服务:
public class MyService : IMyService
{
private readonly IAuxilaryService1 auxilaryService1;
private readonly IAuxilaryService2 auxilaryService2;
private readonly IAuxilaryService3 auxilaryService3;
private IWebService service;
public MyService()
{
auxilaryService1 = new AuxilaryService1();
auxilaryService2 = new AuxilaryService2();
auxilaryService3 = new AuxilaryService3();
}
public void DoSomething(
string Param1,
string Param2
)
{
service = new WebService(auxilaryService1, auxilaryService2, auxilaryService3);
var response = service.DoSomething(Param1, Param2);
/* ... */
}
然后是一个单元测试尝试这样做:
私有模拟网络服务;
[SetUp]
public void SetUp()
{
webService = new Mock<IWebService>();
}
[Test]
public void MyService_DoSomethingTest()
{
IMyService myService = new MyService();
webService.Setup(x => x.DoSomething(It.IsAny<string>(), It.IsAny<string>()))
.Returns(new Response() { Status = "Foo"});
myService.DoSomething("Param1", "Param2");
webService.Verify(x => x.DoSomething(It.IsAny<string>(), It.IsAny<string>()));
}
首先这是测试“MyService”的好方法,其次,由于某种原因,在我的测试中接口没有被正确拦截,即使我确实要求 myService 中的 webService 返回一个虚假响应,实际实例仍然被调用,并且测试失败并出现 Soap 异常。我知道 Moq 需要接口或虚拟方法,但我提供了一个接口 Mock 为什么它不拦截它?有任何想法吗?
我需要补充一点,我正在测试自定义 umbraco 轮廓工作流类型,并且我无法控制构造函数。如果我更改它,项目将会中断。
【问题讨论】:
-
为什么降级了?
标签: web-services unit-testing interface moq