Use FakeItEasy's A.CallTo() on another method in same object

后端 未结 2 689
别那么骄傲
别那么骄傲 2020-12-19 09:54

Using FakeItEasy, how do I check to see if my object\'s method calls another method on this same object?

The Test:

[TestMethod]
public void EatBanana         


        
2条回答
  •  失恋的感觉
    2020-12-19 10:18

    Why are you mocking tested object? What exactly are you trying to test? The verification that call to WillEat happened is of little value. What information does it server to consumer? After all, consumer doesn't care how method is implemented. Consumer cares what are the results.

    What happens when monkey eats banana that is not rotten? Your test should answer this question:

    [TestMethod]
    public void EatBanana_CAUSES_WHAT_WhenBananaIsNotRotten()
    {
        var repo = A.Fake();
        var monkey = new Monkey(repo);
        var freshBanana = new Banana { IsRotten = false };
    
        monkey.EatBanana(freshBanana);
    
        // verifications here depend on what you expect from
        // monkey eating fresh banana
    }
    

    Note that you can make all sort of verifications to IMonkeyRepo, which is properly faked and injected here.

提交回复
热议问题