Skip to main content
 首页 » 编程设计

c#之最小起订量验证表达式

2024年02月23日17lyhabc

美好的一天,

我有一个类执行注册表查找以确定应用程序的安装位置(在 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,

科森

请您参考如下方法:

你真的不需要在这里使用模拟。

您的 sut 似乎是 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); 
}