0

I'm trying to test a method and verify whether another method was called or not. This depends on a bool value. If it's true, the other method should be called. Still, the assertion occasionally works because the true scenario might run before the false scenario, and xUnit counts the call that happens there.

        [Theory]
        [InlineData(false)]
        [InlineData(true)]
        public async Task Test_Resync(bool shouldUpdate)
        {
            ...

            await Resync().ConfigureAwait(false);

            if (shouldUpdate)
            {
                A.CallTo(() => mock.AnyMethod()).MustHaveHappened();
                return;
            }

            A.CallTo(() => mock.AnyMethod()).MustNotHaveHappened();
        }

Although I can use MustHaveHappenedOnceOrLess as a workaround, I think this is not reflecting the behavior expected, so my doubt is: is there a way to have independent cases when using Theory? Would it be better to separate both scenarios?

  • 1
    Do you want the test to run only in specific order? You can't guarantee order using `InlineData` – Chetan Mar 16 '22 at 06:13
  • 2
    In this case, I think there's little value is using a theory. It would probably be cleaner to have two separate tests. However, I'm not sure it would solve your problem, since xUnit runs theory cases as if they were separate tests. Could you show how you declared and initialized `mock`? I suspect it's not recreated between tests. – Thomas Levesque Mar 16 '22 at 10:18
  • Thanks, Thomas, you were right! The mock was not being recreated, ensuring that the issue is fixed :) – Eduardo Luis Santos Mar 17 '22 at 08:46

1 Answers1

1

InlineData ensures independent execution but you can still be using the same mock. The solution is to ensure the mock has been recreated.