NSubstitute:模拟方法未返回预期结果

NSubstitute: Mock method is not returning expected result

我是 NSubstitute、模拟和单元测试的新手。

我正在尝试使用 NSubstitute 删除我在测试中的 class 中的一些依赖项,但是根据我对它们的配置方式,模拟对象中的方法并不像我预期的那样运行.这是我在 Visual Studio:

中创建的示例
  1. 接口和具体class待替换。注意,MyConcreteClass.MyMethod() returns false:

    public interface IMyInterface
    {
        bool MyMethod(string arg);
    }
    
    public class MyConcreteClass : IMyInterface
    {
        public bool MyMethod(string arg)
        {
            return false;
        }
    }
    
  2. 我的class正在测试中:

    public class MyTestedClass
    {
        private IMyInterface _concrete;
    
        public MyTestedClass()
        {
            _concrete = new MyConcreteClass();
        }
    
        public MyTestedClass(IMyInterface mock)
        {
            _concrete = mock;
        }
    
        public bool MyConcreteMethod(string arg)
        {
            return _concrete.MyMethod(arg);
        }
    }
    
  3. 我的单元测试 class MyTestedClass:

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void Given_MyMethodIsUsingAMock_ShouldReturnTrue()
        {
            // Arrange
            var myMock = Substitute.For<IMyInterface>();
            myMock.MyMethod("blah").Returns(true);
            var myTestedObject = new MyTestedClass(myMock);
    
            // Act
            var result = myTestedObject.MyConcreteMethod("blah blah");
    
            // Assert
            Assert.AreEqual(result, true); // This assertion fails!
        }
    
        [TestMethod]
        public void Given_MyMethodIsNotMock_ShouldReturnFalse()
        {
            // Arrange
            var myTestedObject = new MyTestedClass();
    
            // Act
            var result = myTestedObject.MyConcreteMethod("blah blah");
    
            // Assert
            Assert.AreEqual(result, false); // This assertion passes.
        }
    }
    
  4. 测试结果显示Given_MyMethodIsUsingAMock_ShouldReturnTrue()失败:

    MyUnitTests (2 tests) [0:00.190] Failed: 1 test failed
     MyUnitTests (2 tests) [0:00.190] Failed: 1 test failed
      UnitTest1 (2 tests) [0:00.190] Failed: 1 test failed
       Given_MyMethodIsNotMock_ShouldReturnFalse [0:00.000] Success
       Given_MyMethodIsUsingAMock_ShouldReturnTrue [0:00.189] Failed
    Assert.AreEqual failed. Expected:<False>. Actual:<True>. 
       at MyUnitTests.UnitTest1.Given_MyMethodIsUsingAMock_ShouldReturnTrue() in "c:\MyWorkspace\projects\NSubstituteMocking\MyUnitTests\UnitTest1.cs":line 23
    

我好像漏掉了一个微不足道的配置,但它让我望而却步。

MyMethod 被排列成 return true 当给出 "blah"

myMock.MyMethod("blah").Returns(true);

但在执行时会提供 "blah blah"

var result = myTestedObject.MyConcreteMethod("blah blah");

由于 expected/arranged 参数不匹配,模拟未按配置运行。

为模拟提供它期望接收的内容,以便它按预期运行。

[TestMethod]
public void Given_Blah_MyConcreteMethod_ShouldReturnTrue() {
    // Arrange
    var myMock = Substitute.For<IMyInterface>();
    var arg = "blah";
    var expected = true;
    myMock.MyMethod(arg).Returns(expected);
    var myTestedObject = new MyTestedClass(myMock);

    // Act
    var actual = myTestedObject.MyConcreteMethod(arg);

    // Assert
    Assert.AreEqual(expected, actual); // This should pass
}

注意使用变量来存储提供值和期望值,以便在进行测试时减少错误。