web-services - 如何使用 Rhino Mocks 模拟 WCF Web 服务

标签 web-services unit-testing rhino-mocks

如何测试使用由 Web 服务引用生成的代理客户端的类?

我想模拟客户端,但生成的客户端界面不包含 close 方法,这是正确终止代理所必需的。如果我不使用接口(interface),而是使用具体的引用,我可以访问 close 方法但失去模拟代理的能力。

我正在尝试测试与此类似的类:

public class ServiceAdapter : IServiceAdapter, IDisposable
{
    // ILoggingServiceClient is generated via a Web Service reference
    private readonly ILoggingServiceClient _loggingServiceClient; 

    public ServiceAdapter() : this(new LoggingServiceClient()) {}

    internal ServiceAdapter(ILoggingServiceClient loggingServiceClient)
    {
        _loggingServiceClient = loggingServiceClient;
    }


    public void LogSomething(string msg)
    {
        _loggingServiceClient.LogSomething(msg);
    }

    public void Dispose()
    {
        // this doesn't compile, because ILoggingServiceClient doesn't contain Close(), 
        // yet Close is required to properly terminate the WCF client
        _loggingServiceClient.Close(); 
    }
}

最佳答案

我将创建另一个从 ILoggingServiceClient 继承但添加 Close 方法的接口(interface)。然后创建一个包装 LoggingServiceClient 实例的包装类。就像是:

public interface IDisposableLoggingServiceClient : ILoggingServiceClient
{
    void Close();
}

public class LoggingServiceClientWrapper : IDisposableLoggingServiceClient
{
    private readonly LoggingServiceClient client;

    public LoggingServiceClientWrapper(LoggingServiceClient client)
    {
        this.client = client;
    }

    public void LogSomething(string msg)
    {
        client.LogSomething(msg);
    }

    public void Close()
    {
        client.Close();
    }
}

现在您的服务适配器可以使用 IDisposableLoggingServiceClient。

关于web-services - 如何使用 Rhino Mocks 模拟 WCF Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2645620/

相关文章:

web-services - 如何从excel宏调用Web服务

web-services - ajax调用失败时在vb.net中获取xhr对象

web-services - Excel 2007/2010 如何使用 REST Web 服务?

c# - 我可以判断某个属性是否已通过 Rhino Mocks 访问

c# - 使用 UnitofWork 模式的 Rhino 模拟 Entity Framework 不起作用

c# - .NET 中的 PDF 二进制数据输出

c# - 在此示例中,Microsoft是否有权在每个测试中执行多个断言?

html - 未捕获的类型错误 : Cannot read property 'env' of undefined

java - 如何 stub 用@InjectMocks 注释的类的方法?

c# - 你如何用 Rhino Mocks stub IQueryable<T>.Where(Func<T, bool>)?