问题
How can I mock/facke the result from a function that is called in another function? Usually Test2 would be a DataAccess method that I dont like to fetch real data. What I like my unittest to test is the business logic.
This Is what I have now but its not working at all. Sum is always asserted as 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!
}
Then I have a Unittest that I would like to run on my "business logic".
[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);
}
回答1:
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);
}
回答2:
You can't do that as far as I know.
Your instance
is not an instance of the real class, just a mockup on it's interface, hence a call to instance.Test1()
won't call the code you described above. You can however UnitTest Test2
Method on it's own.
What you can do however is, make 2 Unit Tests.
In the first test (Testing Method Test2
), you instantiate your class with the necessary dependencies (or if there are no dependencies with certain values/parameters).
Then do a second test, with same input parameters, and Testing Test()
Method.
Mockups are only used for dependencies where you have to mock up on an interface (which is instantiated outside of the class you test). i.e. if you have ClassA
and ClassB
and ClassA
depends on IClassB
interface. Then you can mock B to test A.
来源:https://stackoverflow.com/questions/21733861/mocking-a-method-within-a-method-with-fakeiteasy