[英]Moq Verify Expression
美好的一天,
我有一个执行注册表查找以确定在哪里安装应用程序(在64位计算机上)的类。
我正在编写一个单元测试,试图验证这一点,这就是我所拥有的:
[Test, Explicit]
public void Validate64Bit()
{
wsMock.Setup(x => x.IsInstalled).Returns(true);
wsMock.Setup(x => x.Path).Returns(@"C:\Program Files (x86)\DIRP\");
IWorkstationLocator workstationLocator = new WorkstationLocator();
string workstationInstallationPath = workstationLocator.Path;
Assert.That(workstationInstallationPath != string.Empty, "The install path should exist.");
wsMock.Verify(x => x.Path == workstationInstallationPath,
"64-bit Workstation Install Path should match: " + @"C:\Program Files (x86)\DIRP\");
}
但是我遇到一个错误:
System.ArgumentException:表达式不是方法调用:x => x.Path == .workstationInstallationPath
所以我的问题是:我想测试x.Path == wrokstationInstallationPath。
我将如何在.Verify()方法中执行此操作?
还是我最好使用断言?
TIA,
子
您实际上不需要在这里使用模拟。
您的输入似乎是WorkstationLocator
类,您所要做的只是检查Path
属性是否等于特定值。
您可以简单地执行以下操作:
[Test, Explicit]
public void Validate64Bit()
{
var expectedPath = @"C:\Program Files (x86)\DIRP\";
IWorkstationLocator workstationLocator = new WorkstationLocator();
Assert.AreEqual(expectedPath, workstationLocator.Path,
"64-bit Workstation Install Path should match: " + expectedPath);
}
Moq的Verify
通常用于验证是否调用了特定方法。 例如,
// Verify with custom error message for failure
mock.Verify(foo => foo.Execute("ping"), "When doing operation X, the service should be pinged always");
如果要测试x.Path == WorkstationInstallationPath,则实际上只是在断言这两个值相同,而没有验证这两个值是由某种方法调用设置的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.