模拟测试时找不到合同名称。

时间:2013-02-18 00:26:24

标签: c# wcf testing moq

我正在试图弄清楚这个错误。我正在使用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文件具有以下绑定

<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]]'.

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

1 个答案:

答案 0 :(得分:1)

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

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