【问题标题】:NSubstitute: Mock method is not returning expected resultNSubstitute:模拟方法未返回预期结果
【发布时间】:2017-12-05 06:51:40
【问题描述】:

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

我正在尝试使用NSubstitute 来删除我在我的测试类中的一些依赖项,但是根据我的配置方式,模拟对象中的方法并没有像我预期的那样运行。这是我在 Visual Studio 中创建的示例:

  1. 要替换的接口和具体类。注意,MyConcreteClass.MyMethod() 返回false:

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

    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. 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
    

看起来我错过了一个琐碎的配置,但它正在躲避我。

【问题讨论】:

    标签: c# .net unit-testing mocking nsubstitute


    【解决方案1】:

    MyMethod 被安排在给定"blah" 时返回true

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

    但是在被执行时提供"blah blah"。

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

    由于预期/安排的参数不匹配,模拟的行为与配置不符。

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

    [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
    }
    

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

    【讨论】:

    • 但是让模拟方法期望一个特定的字符串值与真实方法的行为不匹配。无论arg 的值如何,真正的方法将始终返回false。有没有办法将模拟设置为这样?
    • @mbadawi23 你可以做myMock.MyMethod("").ReturnsForAnyArgs(false)(见docs)。如果您确实需要真实行为,请考虑使用真实对象而不是模拟对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2010-09-22
    • 2021-05-17
    相关资源
    最近更新 更多