3

I'm using fake it easy like so :

var callBackAction = A.Fake<Action<object>>();

//act
token.RegisterChangeCallback(callBackAction, "hi");

//assert
A.CallTo(() => callBackAction.Invoke(A<object>.Ignored)).MustHaveHappened();

and I'm getting the error

    FakeItEasy.ExpectationException : Assertion failed for the following call: 

    System.Action`1[System.Object].Invoke(obj: <Ignored>) Expected to find it 

    exactly once but found it #0 times among the calls: 

          1:     System.Action`1[System.Object].Invoke(obj: "hi)

This seems very odd to me. I could understand if it had found none or if it was something to do with overriding equals() but this is very odd as it has obviously found my call and I'm using an ignored but it is not matching them up. Is this something to do with using actions?

milo.farrell
  • 662
  • 6
  • 19
  • I understand that this may be a simplified example but I see no need for mocking the action here. Just create the delegate and use a flag to verify that it was called. – Nkosi Jul 03 '17 at 11:52
  • Thanks i tried this first but it didn't work due to threading, which turned out to be the problem with fake it easy, the thing that really threw me was the error message. – milo.farrell Jul 03 '17 at 12:18

1 Answers1

4

This was due to the fact that a thread is spawned which waits for a condition and the action is then called in that thread. While in the test this condition is true immediately and so the thread returns very quickly. This is not quick enough for the asset to be true as that happens immediately after the thread was created so the test fails. However after the assert fails and before FakeItEasy has finished collecting the calls that have happened for the error messages the action is called. This causes FakeItEasy to show that it was called as part of its error message despite the fact the test failed due to it not having been called.

This is what i think happens. Diagram of threads

milo.farrell
  • 662
  • 6
  • 19
  • 1
    Your assumption seems correct; I can reproduce the problem (not every time, but sometimes, which is expected since there's a race condition). I opened an issue on Github: https://github.com/FakeItEasy/FakeItEasy/issues/1159 – Thomas Levesque Jul 03 '17 at 13:51