【发布时间】:2014-02-13 07:52:59
【问题描述】:
如何模拟/伪造在另一个函数中调用的函数的结果?通常 Test2 将是我不喜欢获取真实数据的 DataAccess 方法。 我喜欢我的 unittest 测试的是业务逻辑。
这就是我现在所拥有的,但它根本不起作用。 Sum 始终断言为 5!
public int Test1()
{
var value = this.Test2(); //Unittest should substitute with 5
var businesslogic = value + 10; //The business logic
return businesslogic;
}
public int Test2()
{
return 10; //I try to mock this value away in the test. Don´t go here!
}
然后我有一个单元测试,我想在我的“业务逻辑”上运行。
[TestMethod()]
public void TestToTest()
{
//Arrange
var instance = A.Fake<IClassWithMethods>();
//Make calling Test2 return 5 and not 10.
A.CallTo(() => instance.Test2()).Returns(5);
//Call the method
var sum = instance.Test1();
//Assert if the business logic in the method works.
Assert.AreEqual(15, sum);
}
【问题讨论】:
标签: c# unit-testing fakeiteasy