模拟上的所有调用都必须具有相应的设置 [英] All invocation on the mock must have a corresponding setup

查看:61
本文介绍了模拟上的所有调用都必须具有相应的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些遗留代码要进行单元测试.我创建了第一个起订量测试,但出现以下异常:

I have some legacy code I want to unit test. I created a first moq test but I am getting the following exception:

Moq.MockException:IConnection.SendRequest(ADF.Messaging.Contract.ConfigServer.GetDataVersionRequest) 调用因模拟行为严格"而失败.关于的所有调用 模拟必须具有相应的设置.

Moq.MockException:IConnection.SendRequest(ADF.Messaging.Contract.ConfigServer.GetDataVersionRequest) invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup.

重要的代码段:

课堂上的财产

Public Property Connection() As IConnection
    Get
        Return _connection
    End Get
    Set(ByVal value As IConnection)
        _connection = value
    End Set
End Property

应该测试的方法:(_connection)实际上是一个创建tcp套接字的类,我想模拟该属性,以便SendRequest返回我想要的内容.

The method that should be tested: (_connection) is a actually a class that creates a tcp socket and I want to mock that property so the SendRequest returns what I want.

Public Function GetVersion(ByVal appID As Contract.ApplicationID) As Contract.DataVersion
    EnsureConnected()
    Dim req As GetDataVersionRequest = New GetDataVersionRequest(appID)

    Dim reply As CentralServiceReply = _connection.SendRequest(req) //code I want to mock
    Utils.Check.Ensure(TypeOf reply Is GetDataVersionReply, String.Format("Unexpected type: {0}, expected GetDataVersionReply!", reply.GetType()))

    Dim version As Contract.DataVersion = CType(reply, GetDataVersionReply).Version
    version.UpgradeOwners()
    If (Not version.IsSupported) Then
        Return Contract.DataVersion.UNSUPPORTED
    End If

    Return version
End Function

测试方法:

[TestMethod]
public void TestMethod2()
{
    Contract.CentralServiceRequest req = new Contract.ConfigServer.GetDataVersionRequest(new ApplicationID("AMS", "QA"));

    DataVersion v = new DataVersion();
    v.AppVersion = "16";
    CentralServiceReply reply = new GetDataVersionReply(v);

    var ConnectionMock = new Mock<IConnection>(MockBehavior.Strict);
    ConnectionMock.Setup(f => f.SendRequest(req)).Returns(reply);

    var proxy = new ConfigServerProxy(new ApplicationID("AMS", "QA"), "ws23545", 8001);
    proxy.Connection = ConnectionMock.Object; //assign mock object

    DataVersion v2 = proxy.GetVersion(new ApplicationID("AMS", "QA"));
    Assert.AreEqual(v.AppVersion, v2.AppVersion);
}

当我调试单元测试时,我看到在_connection.SendRequest行上执行proxy.GetVersion时,我们得到了错误.同样,当我在监视窗口中观看变量(_connection)时,我看到它也是moq对象.因此,我认为财产分配进行得很顺利.

When I debug the unit test I see that when proxy.GetVersion is executed on the line _connection.SendRequest we get the error. Also when I watch the variable (_connection) in the watch window I see it's the moq object. So I suppose that property assignment went well.

有人看到我错了吗?

推荐答案

我认为问题出在以下方面:

I suppose the problem is in the following thing:

Contract.CentralServiceRequest req = new Contract.ConfigServer.GetDataVersionRequest(new ApplicationID("AMS", "QA"));

代理进行调用以获取应用程序版本,但不使用相同的请求对象(它可能会创建另一个具有相同参数的对象).由于它是不同的对象,并且模拟被设置为期望相同,所以它会失败.

Proxy makes the call to get application version, but doesn't use this same request object (it probably creates another one with same parameters). Since it's different objects and mock is set up to expect the same, it fails.

一个合理的解决方案是期望CentralServiceRequest类型的任何请求.我对Moq不太了解,但我想是这样的:

Reasonable solution would be to expect any request of type CentralServiceRequest. I'm not well versed in Moq, but I suppose it's something like this:

ConnectionMock.Setup(f => f.SendRequest(ItExpr.IsAny<Contract.CentralServiceRequest>())).Returns(reply);

希望这会有所帮助.

这篇关于模拟上的所有调用都必须具有相应的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆