gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:28:23 24 4
gpt4 key购买 nike

我对 NSubstitute、模拟和单元测试总体来说是新手。

我正在尝试使用 NSubstitute 删除测试类中的一些依赖项,但模拟对象中的方法的行为并不符合我的预期(根据我的配置方式)。以下是我在 Visual Studio 中创建的示例:

  1. 要替换的接口(interface)和具体类。注意,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

看起来我错过了一个微不足道的配置,但它却在逃避我。

最佳答案

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
}

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

关于c# - NSubstitute:模拟方法未返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47647706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com