Rhino Mocks DynamicMultiMock - 在附加接口上设置带有返回值的期望值

时间:2010-03-11 16:33:35

标签: c# .net unit-testing rhino-mocks

我正在创建一个DynamicMultiMock,如下所示:

this.serviceClient = this.mocks.DynamicMultiMock<ISlippyPlateProcedureService>(typeof(ICommunicationObject));

然后设置以下期望:

Expect.Call(((ICommunicationObject)this.serviceClient).State).Return(CommunicationState.Faulted);

当我执行测试时,Rhino Mocks报告了这一点:
重播期望:ICommunicationObject.get_State();
动态模拟:忽略意外的方法调用:ICommunicationObject.get_State();

我是否正确设定了这种期望还是有另一种方式?

编辑以包含完整的测试代码:

        Expect.Call(this.syncContextContainer.SynchronizationContext).Return(new SlippyPlateProcedureSynchronizationContextMock());
        Expect.Call(this.clientFactory.CreateServiceClient(null)).IgnoreArguments().Return(this.serviceClient);
        Expect.Call(((ICommunicationObject)this.serviceClient).State).Return(CommunicationState.Faulted);
        Expect.Call(() => this.serviceClient.IsAlive());
        this.mocks.ReplayAll();
        SlippyPlateProcedureClient client = new SlippyPlateProcedureClient(
            this.syncContextContainer, this.clientFactory, this.container);
        PrivateObject privateObject = new PrivateObject(client);
        SlippyPlateProcedureClient_Accessor target = new SlippyPlateProcedureClient_Accessor(privateObject);

        target.CheckServerConnectivity();

        this.mocks.VerifyAll();

由于

安德烈

1 个答案:

答案 0 :(得分:2)

在没有看到您的生产代码的情况下,很难说出现了什么问题。以下测试通过:

public interface IA
    {
        int A(int a);
    }

    public interface IB
    {
        int B(int b);
    }

    [Test]
    public void Multimocks()
    {
        MockRepository mocks = new MockRepository();
        IA mymock = mocks.DynamicMultiMock<IA>(typeof (IB));
        Expect.Call(mymock.A(1)).Return(2);
        Expect.Call(((IB)mymock).B(3)).Return(4);
        mocks.ReplayAll();

        Assert.AreEqual(2, mymock.A(1));
        Assert.AreEqual(4, ((IB)mymock).B(3));
    }

表示多模式正常工作。你确定你不是多次给国家打电话吗?尝试将你的模拟更改为StrictMocks(当不期望调用时会出现异常),所以:

this.serviceClient = this.mocks.StrictMultiMock<ISlippyPlateProcedureService>(typeof(ICommunicationObject));

并多次读取属性:

Expect.Call(((ICommunicationObject)this.serviceClient).State).Return(CommunicationState.Faulted).Repeat.Any();

让我知道你的进步。