【问题标题】:Is there any way to mock a request for websocket有什么方法可以模拟对 websocket 的请求
【发布时间】:2019-07-22 18:31:02
【问题描述】:

我有一个从不同服务接收数据的应用程序。 我正在编写一个测试方法来验证应用程序是否删除了所有外部依赖项。 我正在使用 Fiddler 拦截从客户端发送到服务器的请求并发送我自定义的 json 响应。它可以按预期处理 http(s) 请求,但不能处理 WEBSOCKETS。

我可以在 C# 中使用任何其他工具来模拟 Websocket 连接的请求/响应吗?

【问题讨论】:

    标签: c# websocket mocking


    【解决方案1】:

    一种典型的方法是将调用包装在一个可模拟的接口调用中。

    interface IWebSocketCaller
    {
       Result GetData(); 
    }
    

    在您的实际课程和模拟课程中实现这一点。 你也可以使用 Moq 之类的模拟框架来获取这个模拟类

    class MockWebCaller: IWebSocketCaller
    {
        Result GetData()
        {
           // dummy result
            return new Results();
        }
    }
    

    现在假设你的调用类是这样的。

    Class CleintClass{
    IWebSocketCaller  _caller; 
     ClientClass(IWebSocketCaller caller)
     { 
        this._caller = caller; 
     }
     Result GetData()
     {
        return  this._caller.GetData();
      }
    }
    

    现在在测试方法中使用已模拟调用者类的客户端类的实例。 希望这可以帮助。

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 2012-02-18
      • 2014-03-18
      • 1970-01-01
      相关资源
      最近更新 更多