Skip to content

Commit

Permalink
Uninvoke SequenceSetup on mock.Invocations.Clear() (#896)
Browse files Browse the repository at this point in the history
* Add failing test: `mock.Invocations.Clear()` does not reset sequence setups
* Fix test by specializing `SequenceSetup.Uninvoke()`
  • Loading branch information
stakx authored Aug 17, 2019
1 parent 44a46d2 commit bd7ebaf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Moq/SequenceSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public override MockException TryVerifyAll()
return this.invoked ? null : MockException.UnmatchedSetup(this);
}

public override void Uninvoke()
{
this.invoked = false;
}

private readonly struct Response
{
private readonly ResponseKind kind;
Expand Down
15 changes: 14 additions & 1 deletion tests/Moq.Tests/InvocationsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void Invocations_for_object_methods_on_interface_proxy_record_return_valu
}

[Fact]
public void Invocations_Clear_also_resets_setup_verification_state()
public void Invocations_Clear_also_resets_setup_verification_state_of_regular_setups()
{
var mock = new Mock<IComparable>();
mock.Setup(m => m.CompareTo(default));
Expand All @@ -142,6 +142,19 @@ public void Invocations_Clear_also_resets_setup_verification_state()
Assert.Equal(MockExceptionReasons.UnmatchedSetup, ex.Reasons);
}

[Fact]
public void Invocations_Clear_also_resets_setup_verification_state_of_sequence_setups()
{
var mock = new Mock<IComparable>();
mock.SetupSequence(m => m.CompareTo(default));
_ = mock.Object.CompareTo(default);
mock.VerifyAll(); // ensure setup has been matched

mock.Invocations.Clear();
var ex = Assert.Throws<MockException>(() => mock.VerifyAll());
Assert.Equal(MockExceptionReasons.UnmatchedSetup, ex.Reasons);
}

[Fact]
[Obsolete()]
public void Invocations_Clear_resets_count_kept_by_setup_AtMost()
Expand Down

0 comments on commit bd7ebaf

Please sign in to comment.