'xUnit is using a previous iteration to evaluate a different case when using Theory and InlineData

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eduardo Luis Santos