繁体   English   中英

模拟WCF服务的问题

[英]Issue with mocking WCF service

我在模拟WCF服务时遇到一些问题:

1)我声明了一个带有空方法的类,该方法仅实现我的服务接口:

public class MyFakeService : IMyService {
   ...
   public virtual MyResult GetResult(MyResponse response){
      throw new NotImplementedException();
   };
}

2)我有MyResponse类:

public class MyResponse {
      public long myField;
   }

3)我创建了服务类的模型和服务主机来托管此伪造的服务:

myFakeService = mocks.StrictMock<MyFakeService>();
ServiceHost host = new ServiceHost(myFakeService);

(这里我省略了端点配置等)

4)现在我尝试测试我的客户。 client.GetSomethingFromService()方法准确地调用服务的GetResult(MyResponse)方法。

With.Mocks(mocks)
    .Expecting(() => Expect
        .Call(myFakeService.GetResult(null))
        .IgnoreArguments()
        .Constraints(PublicField.Value("myField", 777))
        .Return(new MyResult()))
    .Verify(() => myClient.GetSomethingFromService());

问题是,如果服务中出现问题,我只能看到以下内容:

System.ServiceModel.CommunicationObjectFaultedException: 
 The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used
 for communication because it is in the Faulted state.

我怎么知道到底是什么错? 也许参数约束验证失败或其他原因?

谢谢。

首先,避免使用严格的模拟。 这是一种不好的做法,因为它们会使您的测试过于脆弱。

其次,如果您要测试WCF服务,则无需启动ServiceHost,因为您随后将进行集成测试。 您只想测试服务的逻辑,而不是WCF基础结构。

有关如何使用RhinoMocks和WCF服务的完整介绍,请参阅我有关单元测试WCF服务的博客文章。

暂无
暂无

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

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