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
The following Nunit test should pass, but it fails on the last assertion. This was discovered in .Net 4.5 with Moq 4.2.1510.2205 and is still an issue in Moq 4.5.7.
using Moq;
using NUnit.Framework;
[TestFixture]
public class MyMoqTests {
[Test]
public void ShouldHandleStructSetups()
{
// Set up objects.
var mock = new Mock<IMyService>();
var struct1 = new MyStruct { Member = 1 };
var struct2 = new MyStruct { Member = 2 };
// Basic assertions about struct equality. These all pass.
Assert.AreNotEqual(struct1, struct2);
Assert.AreEqual(struct1, new MyStruct { Member = 1 });
Assert.AreEqual(struct2, new MyStruct { Member = 2 });
// Do the mock setups. The second call overwrites the first, even
// though struct1 != struct2
mock.Setup(x => x.Call(struct1)).Returns("first");
mock.Setup(x => x.Call(struct2)).Returns("second");
// Execute the setups.
var first = mock.Object.Call(struct1);
var second = mock.Object.Call(struct2);
// Verify that both calls were made exactly once. Equality works
// correctly during verify. These tests both pass.
mock.Verify(x => x.Call(struct1), Times.Once);
mock.Verify(x => x.Call(struct2), Times.Once);
// Assert that the setups executed correctly.
Assert.AreEqual("second", second); // => passes
Assert.AreEqual("first", first); // => fails; expected "first" but was null
}
public interface IMyService
{
string Call(MyStruct param);
}
public struct MyStruct
{
public int? Member;
}
}
Important notes:
If MyStruct#Member is just an int instead of an int?, this test passes.
If MyStruct implements an equality operator, this test passes.
If the setups are instead performed with It.Is<MyStruct>(y => y.Member == struct1.Member) (struct2.Member for the second setup), this test passes.
However, if the equality function is pulled out into a method, such as private bool Eq(MyStruct s1, MyStruct s2) { return s1.Member == s2.Member; } or the standard Object#Equals, this test still fails.
The text was updated successfully, but these errors were encountered:
The following Nunit test should pass, but it fails on the last assertion. This was discovered in .Net 4.5 with Moq 4.2.1510.2205 and is still an issue in Moq 4.5.7.
Important notes:
MyStruct#Member
is just anint
instead of anint?
, this test passes.MyStruct
implements an equality operator, this test passes.It.Is<MyStruct>(y => y.Member == struct1.Member)
(struct2.Member
for the second setup), this test passes.private bool Eq(MyStruct s1, MyStruct s2) { return s1.Member == s2.Member; }
or the standardObject#Equals
, this test still fails.The text was updated successfully, but these errors were encountered: