You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[TestClass]
public class IsThisABug
{
[TestMethod]
public void SaverSavesTwice()
{
var mock = new Mock<ISaver>();
var saver = mock.Object;
var thing = new Thing { Value = "A" };
saver.Save(thing);
thing.Value = "B";
saver.Save(thing);
mock.Verify(s => s.Save(It.Is<Thing>(t => t.Value == "A")));
mock.Verify(s => s.Save(It.Is<Thing>(t => t.Value == "B")));
}
public interface ISaver
{
long Save(Thing thing);
}
public class Thing
{
public string Value { get; set; }
}
}
The first Verify call fails, output says that Value == "B" was executed twice.
The text was updated successfully, but these errors were encountered:
This is by design. Moq observes the usual .NET equality rules (object.Equals), so by default, reference types are compared by reference/identity. You can either turn your type into a struct, C# 9+ record, or use a custom Capture matcher to inspect/copy state at the time of invocation.
I expect this test to pass:
The first
Verify
call fails, output says thatValue == "B"
was executed twice.The text was updated successfully, but these errors were encountered: