Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 模拟上的所有调用都必须有相应的设置_.net_Unit Testing_Mocking_Moq_Mstest - Fatal编程技术网

.net 模拟上的所有调用都必须有相应的设置

.net 模拟上的所有调用都必须有相应的设置,.net,unit-testing,mocking,moq,mstest,.net,Unit Testing,Mocking,Moq,Mstest,我有一些遗留代码要进行单元测试。我创建了第一个moq测试,但出现以下异常: Moq.MockException:IConnection.SendRequest(ADF.Messaging.Contract.ConfigServer.GetDataVersionRequest) 调用失败,模拟行为严格。上的所有调用 模拟必须有相应的设置 重要代码: 类上的属性: Public Property Connection()作为IConnection 得到 返回连接 结束 设置(ByVal值作为ICon

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

Moq.MockException:IConnection.SendRequest(ADF.Messaging.Contract.ConfigServer.GetDataVersionRequest) 调用失败,模拟行为严格。上的所有调用 模拟必须有相应的设置

重要代码:

类上的属性:

Public Property Connection()作为IConnection
得到
返回连接
结束
设置(ByVal值作为IConnection)
_连接=值
端集
端属性
应该测试的方法:(_connection)实际上是一个创建tcp套接字的类,我想模拟该属性,以便SendRequest返回我想要的内容

公共函数GetVersion(ByVal appID作为Contract.ApplicationID)作为Contract.DataVersion
重新连接的
Dim req As GetDataVersionRequest=新GetDataVersionRequest(appID)
Dim reply As CentralServiceReply=\u connection.SendRequest(req)//我要模拟的代码
Utils.Check.sure(回复类型为GetDataVersionReply,String.Format(“意外类型:{0},应为GetDataVersionReply!”,reply.GetType()))
Dim版本为Contract.DataVersion=CType(reply,GetDataVersionReply)。版本
version.UpgradeOwners()
如果(不是版本。IsSupported),则
返回Contract.DataVersion.UNSUPPORTED
如果结束
返回版本
端函数
试验方法:

[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);
}
[TestMethod]
公共void TestMethod2()
{
Contract.CentralServiceRequest req=新Contract.ConfigServer.GetDataVersionRequest(新应用程序ID(“AMS”、“QA”);
DataVersion v=新的DataVersion();
v、 AppVersion=“16”;
CentralServiceReply reply=new GetDataVersionReply(v);
var ConnectionMock=newmock(MockBehavior.Strict);
ConnectionMock.Setup(f=>f.SendRequest(req)).Returns(reply);
var proxy=新的ConfigServerProxy(新的应用程序ID(“AMS”、“QA”)、“ws23545”、8001);
proxy.Connection=ConnectionMock.Object;//分配模拟对象
DataVersionV2=proxy.GetVersion(新的应用程序ID(“AMS”、“QA”);
Assert.AreEqual(v.AppVersion,v2.AppVersion);
}
当我调试单元测试时,我看到在_connection.SendRequest行上执行proxy.GetVersion时,我们得到了错误。另外,当我在观察窗口中观察变量(_connection)时,我看到它是moq对象。所以我想财产转让进展顺利


有人看到我哪里出错了吗

我想问题在于以下几点:

Contract.CentralServiceRequest req = new Contract.ConfigServer.GetDataVersionRequest(new ApplicationID("AMS", "QA"));
代理调用获取应用程序版本,但不使用相同的请求对象(它可能创建另一个具有相同参数的对象)。因为它是不同的对象,并且mock被设置为期望相同的对象,所以它失败了

合理的解决方案是期望任何类型为CentralServiceRequest的请求。我对Moq不太熟悉,但我想是这样的:

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

希望这有帮助。

Returns应该返回您想要返回的东西(Returns(response))。还有更复杂的方法来处理收到的变量(比如请求应该将某些属性设置为某物)。查看一些文档/教程,看看可以做些什么。Dmitry,我在这里也遇到了同样的问题。谢谢你的帮助!