FakeItEasy: Reset Fake Call History / Ignore Call

I would like to reset the fake call history or ignore the call.

The fake approved method is called in the Init method in the tested constructor of the class, and I want to ignore this call because it is not part of the test action.

Here is an example:

[TestClass]
public class UnitTest1
{
    private MyFakedClass myFakedObject;
    private SUT sut;

    [TestInitialize]
    public void Init()
    {
        myFakedObject = A.Fake<MyFakedClass>();
        sut = new SUT(myFakedObject); //constructor calls myFakedObject.AssertedMethod()
    }

    [TestMethod]
    public void TestMethod1()
    {
        sut.TestedMethod(); //TestedMethod calls myFakedObject.AssertedMethod() again...
        A.CallTo(() => myFakedObject.AssertedMethod()).MustHaveHappened(Repeated.Exactly.Once); 
//...So this is false
    }
}
+4
source share
2 answers

I love @forsvarir's answer. I would be inclined to think that calling from the constructor matters.

However, if you really need to do this:

When the version of FakeItEasy is ≥ 3.2.0 , you can use ClearRecordedCalls:

Fake.ClearRecordedCalls(fake);

2.0.0 ≤ FakeItEasy version < 3.2.0, forsvarir (FakeItEasy: Reset Fake Calls History/Ignore Call).

FakeItEasy < 2.0.0, :

[Test]
public void TestMethod1()
{
    using (Fake.CreateScope())
    {
        sut.TestedMethod(); // calls myFakedObject.AssertedMethod() again
        A.CallTo(() => myFakedObject.AssertedMethod())
         .MustHaveHappened(Repeated.Exactly.Once);
    }
}

MustHaveHappened, , .

TestInitialize TestCleanup, using .

+7

. , . , , AssertedMethod, , ? SUT , ?

- -, reset , , Ive, , , . , , , - , , TestClass, , /init :

const int InitAssertedMethodCalls = 1;

[TestMethod]
public void TestMethod1()
{
    sut.TestedMethod(); 
    A.CallTo(() => myFakedObject.AssertedMethod())
             .MustHaveHappened(Repeated.Exactly.Times(1 + InitAssertedMethodCalls )); 
}
+3

All Articles