【问题标题】:Moq. Mock delegate input起订量。模拟委托输入
【发布时间】:2015-08-22 08:30:02
【问题描述】:

我正在尝试使用 Moq 模拟委托,但到目前为止我所做的一切都是徒劳的。 我做了一个小例子来说明设置的方式:

1.代理类正在抽象 WCF 服务

public interface IServiceProxy
{
    void FooService(ServiceProxy.FooServiceDelegate service);
}

public class ServiceProxy : IServiceProxy
{
    private readonly EndpointAddress _fooServiceEndpoint;

    public ServiceProxy(IConfiguration configuration)
    {
        _fooServiceEndpoint = new EndpointAddress(new Uri(configuration.WcfServiceEndpoint));
    }

    public delegate void FooServiceDelegate(IFooBar proxy);


    public void FooService(FooServiceDelegate service)
    {
        Do<IFooBar>((service.Invoke), _fooServiceEndpoint);
    }


    private void Do<T>(UseServiceDelegate<T> f, EndpointAddress address)
    {
        UsingService<T>.Use(f.Invoke, address);
    }
}

2.服务定义

/// <summary>
/// This is the interface the WCF service exposes
/// </summary>
public interface IFooBar
{
    string Hello(string name);
}

3.以及使用代理的类

public class DoFoo
{
    private readonly IServiceProxy _serviceProxy;

    public DoFoo(IServiceProxy serviceProxy)
    {
        _serviceProxy = serviceProxy;
    }


    public string SayHello(string name)
    {
        var response = string.Empty;
        _serviceProxy.FooService(proxy => { response = proxy.Hello(name); });

        return response;
    }
}

我想测试在委托FooServiceDelegate 上使用输入IFooBar 定义的方法实际上是否被调用,并且我希望能够通过IFooBar 模拟方法调用的响应委托。

到目前为止,我的尝试都是徒劳的,所以我希望有一些起订量专家可以在这里提供帮助。

这是一个测试示例,当然不能像现在这样工作。

[TestClass()]
public class DoFooTests
{
    [TestMethod, TestCategory("Unit")]
    public void SayHelloJohn_ShouldUseServiceProxy()
    {//SETUP
        var serviceMock = new Mock<IFooBar>(MockBehavior.Loose);
        var delegateMock = new Mock<ServiceProxy.FooServiceDelegate>(MockBehavior.Strict);
        var serviceProxyMock = new Mock<IServiceProxy>(MockBehavior.Strict);
        serviceProxyMock.Setup(m => m.FooService(delegateMock.Object));
        var name = "John";
        var response = $"Hello {name}";

        //Im trying something like this (of course this does not work...)
        delegateMock.Setup(m => m.Hello(name)).Returns(response);

        //EXECUTE
        var doFoo = new DoFoo(serviceProxyMock.Object);
        var result = doFoo.SayHello(name);
        //ASSERT
        // Do some assertions here....
    }
}

【问题讨论】:

    标签: c# unit-testing delegates mocking moq


    【解决方案1】:

    你必须在FooService被执行时调用代理:

    [TestMethod]
    public void SayHelloJohn_ShouldUseServiceProxy()
    {
        const string EXPECTED_RESULT = "Hello John";
        const string NAME = "John";
    
        var fakeServiceProxy = new Mock<IServiceProxy>(MockBehavior.Strict);
        var fakeFooBar = new Mock<IFooBar>(MockBehavior.Loose);
        fakeFooBar.Setup(bar => bar.Hello(NAME)).Returns(EXPECTED_RESULT);
    
        fakeServiceProxy.Setup(proxy => proxy.FooService(
                        It.IsAny<ServiceProxy.FooServiceDelegate>()))
                        .Callback<ServiceProxy.FooServiceDelegate>(arg => arg(fakeFooBar.Object));
    
        var target = new DoFoo(fakeServiceProxy.Object);
    
    
        var result = target.SayHello(NAME);
    
        Assert.AreEqual(EXPECTED_RESULT, result);
    }
    

    上面的sn-p在调用SayHello方法的时候执行proxy =&gt; { response = proxy.Hello(name); })

    【讨论】:

    • 这正是我需要的答案。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 2013-07-18
    • 2022-01-13
    • 2011-09-02
    • 1970-01-01
    • 2022-01-13
    • 2019-10-25
    相关资源
    最近更新 更多