如何对WFC Web套接字进行单元测试

时间:2016-11-10 22:57:54

标签: c# wcf unit-testing websocket

我正在尝试创建一个单元测试,以便逐步执行我的代码,以便访问当我尝试通过Entity Framework访问数据时发生的错误。我没有从搜索答案中找到很好的例子。我尝试在代码的第二行创建一个客户端,当我检查调试器中的client.InnerChannel时,我得到一个“client.InnerChannel抛出System.ServiceModel.CommunicationObjectFaultedException类型的异常”。 WebSocket在我的客户端中可以很好地完成本教程中的简单示例,但是当我尝试通过Entity Framework访问数据库时,会抛出一个我无法检查的错误。

我正在处理本教程中提供的代码http://www.codeproject.com/Articles/619343/Using-WebSocket-in-NET-Part

我的界面定义如下:

namespace PriceBlotterWebSocket
{
    [ServiceContract(CallbackContract = typeof(IPriceBlotterCallback))]
    public interface IPriceBlotterWebSocket
    {
        [OperationContract(IsOneWay = true, Action = "*")]
        Task SendMessageToServer(Message msg);
    }

    [ServiceContract]
    public interface IPriceBlotterCallback
    {
        [OperationContract(IsOneWay = true, Action = "*")]
       Task SendMessageToClient(Message msg);
    }

}

我的单元测试:

[TestClass]
public class UnitTest1
{
    internal class CallBackHandler : IPriceBlotterCallback
    {
        public async Task SendMessageToClient(Message msg)
        {
            System.Diagnostics.Debug.Write(msg);
        }
    }

    [TestMethod]
    public void TestMethod1()
    {
        var context = new InstanceContext(new CallBackHandler());
        var client = new PriceBlotteWebSocketService.PriceBlotterWebSocketClient("CustomBinding_IPriceBlotterWebSocket");
        //Why can't I call client.SendMessageToServer ? 
    }

}

测试项目客户端配置:

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding_IPriceBlotterWebSocket">
                    <textMessageEncoding messageVersion="Soap12" />
                    <httpTransport>
                        <webSocketSettings transportUsage="Always" subProtocol="soap" />
                    </httpTransport>
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="ws://localhost:54426/Service1.svc" binding="customBinding"
                bindingConfiguration="CustomBinding_IPriceBlotterWebSocket"
                contract="PriceBlotteWebSocketService.IPriceBlotterWebSocket"
                name="CustomBinding_IPriceBlotterWebSocket" />
        </client>
    </system.serviceModel>
</configuration>

0 个答案:

没有答案