Moq-Sequences icon indicating copy to clipboard operation
Moq-Sequences copied to clipboard

Past setups should not be checked after moving on to next sequence steps

Open gmkado opened this issue 1 year ago • 0 comments

I'm trying to verify that a call is not made before another call in a sequence. (It is actually made afterwards, not part of what I want to test but is causing the issue).

Here is a repro:

    [Test]
    public void SequenceTest()
    {
        var bar = new Mock<IBar>();
        var baz = new Mock<IBaz>();
        var sut = new Foo(bar.Object, baz.Object);
        using (Sequence.Create())
        {
            baz.Setup(x => x.DoBazThings()).InSequence(Times.Never());
            bar.Setup(x => x.DoBarThings()).InSequence(Times.Once());
            // baz.Setup(x => x.DoBazThings()).InSequence(Times.Once()); // uncomment this to make this test pass

            sut.DoTheThing();
        }
    }

    public interface IBar
    {
        void DoBarThings();
    }

    public interface IBaz
    {
        void DoBazThings();
    }

    private class Foo
    {
        private readonly IBar _bar;
        private readonly IBaz _baz;

        public Foo(IBar bar, IBaz baz)
        {
            _bar = bar;
            _baz = baz;
        }

        public void DoTheThing()
        {
            _bar.DoBarThings();
            _baz.DoBazThings();
        }
    }

This currently fails with:

Moq.Sequences.SequenceException : Exceeded maximum number of invocations.
Expected invocation on the mock should never have been performed, but was 1 times: 'x => x.DoBazThings()'

I would expect that once a condition for a sequence has been completed that it would stop checking it, like when DoBarThings is called, the requirement that DoBazThings is never called would be "cleared".

gmkado avatar Oct 09 '24 23:10 gmkado