Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setups using struct with nullable member overwrite previous setups #263

Closed
meustrus opened this issue May 26, 2016 · 1 comment
Closed

Comments

@meustrus
Copy link

meustrus commented May 26, 2016

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:

  1. If MyStruct#Member is just an int instead of an int?, this test passes.
  2. If MyStruct implements an equality operator, this test passes.
  3. 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.
@stakx
Copy link
Contributor

stakx commented Jun 20, 2017

Hi @meustrus, this has been fixed by #363. Update to Moq 4.7.49 or newer and the problem should go away. :)

@stakx stakx closed this as completed Jun 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants