c# - Mock 测试时找不到合约名称。

标签 c# wcf testing moq

我正在兜圈子试图找出这个错误。我正在使用最小起订量框架执行模拟测试。测试代码为

[TestFixture]
[ServiceContract]
public class UnitTest1
{

    private ServiceHost host;
    [Test]
    public void TestMethod()
    {
        Mock mk = new Mock<ChatInterfaces.IChatService>();
        host = new ServiceHost(mk);
        host.AddServiceEndpoint(typeof(IChatService), new NetTcpBinding()
            , "net.tcp://localhost:8080/ChatService");
        host.Open();
        Console.WriteLine("Testing");

App.Config 文件具有以下绑定(bind)

<system.serviceModel>
    <services>
      <service name="ChatInterfaces.IChatService">
        <endpoint address="net.tcp://localhost:8080/ChatService" binding="netTcpBinding" bindingConfiguration="BindingConfiguration" name="ChatServiceEndPoint" contract="ChatInterfaces.IChatService">
        </endpoint>
      </service>
    </services>

    <bindings>
      <netTcpBinding>
        <binding name="BindingConfiguration" transferMode="Buffered"/>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>

当我运行它时,出现以下错误。

Result Message: System.InvalidOperationException : The contract name     'ChatInterfaces.IChatService' could not be found in the list of contracts implemented by the service 'Moq.Mock`1[[ChatInterfaces.IChatService, ChatInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.

我在这里做错了什么以及如何解决它?谢谢

最佳答案

尝试使用 mk.Object实际得到 IChatService实现,而不是 Mock<T>对象。

Mock<ChatInterfaces.IChatService> mk = new Mock<ChatInterfaces.IChatService>();
host = new ServiceHost(mk.Object);

关于c# - Mock 测试时找不到合约名称。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14927780/

相关文章:

wcf - 使用 WIF/STS 保护 WCF 服务 - 使用所需声明装饰方法?

javascript - karma 测试中的未知 vendor

javascript - 混淆模拟无状态 React 组件辅助函数与 sinon、enyzme 和 ES6 导入

c# - 如何指定只能从同一程序集的派生类访问的方法?

c# - 如何调用自定义控件中存在的控件事件?

c# - 进行 WCF 服务调用的单元测试方法?

c# - 为什么我的无效 WCF 消息没有被记录?

java - 如何使用 Play Framework 测试电子邮件逻辑

c# - 如何使用 Entity Framework 执行存储过程并从存储过程中获取输出

c# - 使用 TimeSpan.Parse 的 QueryExtender Linq Orderby