NSubstitute:Mock方法未返回预期结果 [英] NSubstitute: Mock method is not returning expected result
问题描述
我是NSubstitute,模拟和单元测试的新手.
I'm new to NSubstitute, mocking, and unit testing in general.
我正在尝试使用NSubstitute
删除我在测试中的类中具有的某些依赖关系,但是根据我对它们的配置方式,模拟对象中的方法表现不佳.这是我在Visual Studio中创建的示例:
I'm trying to use NSubstitute
to remove some dependencies I have in my class under test, but methods in the mock objects are not behaving as I expect based on how I configured them. Here is an example I created in Visual Studio:
-
要替换的接口和具体类.注意,
MyConcreteClass.MyMethod()
返回false
:
public interface IMyInterface
{
bool MyMethod(string arg);
}
public class MyConcreteClass : IMyInterface
{
public bool MyMethod(string arg)
{
return false;
}
}
我的课程正在测试:
My class under test:
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);
}
}
我的MyTestedClass
单元测试类:
My unit test class for 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.
}
}
测试结果表明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
看起来我缺少一个琐碎的配置,但它使我难以理解.
It looks like I'm missing a trivial configuration, but it is eluding me.
推荐答案
MyMethod
被安排为在给出"blah"
myMock.MyMethod("blah").Returns(true);
,但是在执行时会为其提供"blah blah"
.
but is then provide it with "blah blah"
when being acted upon.
var result = myTestedObject.MyConcreteMethod("blah blah");
由于预期/安排的参数不匹配,因此该模拟的行为与配置不符.
As the expected/arranged parameter did not match, the mock did not behave as configured.
为模拟提供预期的效果,以使其表现出预期的效果.
Provide the mock with what it expects to receive for it to behave as expected.
[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
}
请注意使用变量存储提供的和期望的值,以便在执行测试时可以减少错误.
Note the use of variables to store the provided and expected values so that mistakes can be reduced when exercising the test.
这篇关于NSubstitute:Mock方法未返回预期结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!