別の関数で呼び出された関数からの結果をモック/偽造するにはどうすればよいですか? 通常、Test2 は、実際のデータを取得したくない DataAccess メソッドになります。単体テストでテストしたいのは、ビジネス ロジックです。
これは私が今持っているものですが、まったく機能していません。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);
}