gpt4 book ai didi

c# - Rhino Mocks DynamicMultiMock - 在附加接口(interface)上设置期望值和返回值

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:38 25 4
gpt4 key购买 nike

我正在创建一个 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();

谢谢

安德烈

最佳答案

如果不查看您的生产代码,真的很难判断出了什么问题。以下测试通过:

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));
}

这意味着 multimocks 工作正常。您确定您不会多次调用 State 吗?尝试将您的模拟更改为 StrictMocks(当不期望调用时您将得到一个异常),因此:

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

并让该属性被多次读取:

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

让我知道你的进展情况。

关于c# - Rhino Mocks DynamicMultiMock - 在附加接口(interface)上设置期望值和返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2426786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com