First, let me say that I think there are fantastic points in Tseng's answer, especially about how
- Faking an interface means the "inner methods" can never be called, and
- You should be faking dependencies, not internals. If you have the option to make this change, do it, and stop reading my answer right now.
If you're still reading, I'm confused about two things:
- should
Test1
returnbusinessLogic
? As it's written (once the compilation errors are fixed), I would expectTest1
to return 5 (not 15) whenTest2
returns 5. - In
TestToTest
, when you set theReturns(5)
onTest2
, and then callTest1
, since you faked an interface, I would expect sum to be 0, the default value for an int. I'm not sure how you'd get 5. In fact, I've replicated this behaviour in this test:
[TestMethod]
public void TestToTestInterface()
{
//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(0, sum); // because Test1 wasn't faked
}
While I don't care for the approach myself, if you really want to have your replaceable code in ClassWithMethods
, and then to test the Test1
method, there's a way:
- We need to make
Test1
andTest2
virtual
, or they won't be fakeable. - Fake out
ClassWithMethods
, notIClasssWithMethods
. - Tell the fake that when
Test1
is invoked, it should call the original code (which will in turn call the fakeTest2
).
I've put these changes all together, and this test passes for me:
public class ClassWithMethods : IClassWithMethods
{
public virtual int Test1()
{
var value = this.Test2(); //Unittest should substitute with 5
var businesslogic = value + 10; //The business logic
return businesslogic;
}
public virtual int Test2()
{
return 10; //I try to mock this value away in the test. Don´t go here!
}
}
[TestMethod]
public void TestToTestClass()
{
//Arrange
var instance = A.Fake<ClassWithMethods>();
//Make calling Test2 return 5 and not 10.
A.CallTo(() => instance.Test2()).Returns(5);
// Make sure that Test1 on our fake calls the original ClassWithMethods.Test1
A.CallTo(() => instance.Test1()).CallsBaseMethod();
//Call the method
var sum = instance.Test1();
//Assert if the business logic in the method works.
Assert.AreEqual(15, sum);
}