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

Setup return value fails in a constellation with nullable ints #1223

Closed
Kiechlus opened this issue Dec 7, 2021 · 2 comments
Closed

Setup return value fails in a constellation with nullable ints #1223

Kiechlus opened this issue Dec 7, 2021 · 2 comments

Comments

@Kiechlus
Copy link

Kiechlus commented Dec 7, 2021

Using moq version 4.16.1

Setting a return value does not work when an optional nullable int? is setup as It.IsAny<int>(). However, the compiler does not complain neither. Finding out what was the problem took us quite some time, so this can be a real annoyance.

public class MockedClass
{
    public virtual string MockedMethod(int? pageIndex = null) => "test";
}

public class ClassToTest
{
    private readonly MockedClass mockedProperty = null;

    public ClassToTest(MockedClass mockedProperty)
    {
        this.mockedProperty = mockedProperty;
    }

    public string SomeMethod()
    {
        return mockedProperty.MockedMethod();
    }
}

[Fact]
public void Test()
{
    // Works as expected
    var mock = new Mock<MockedClass>();
    mock.Setup(x => x.MockedMethod(It.IsAny<int?>())).Returns("foobar");
    var classToTest = new ClassToTest(mock.Object);
    Assert.Equal("foobar", classToTest.SomeMethod()); // This works

    // Does not work
    var mock2 = new Mock<MockedClass>();
    mock2.Setup(x => x.MockedMethod(It.IsAny<int>())).Returns("foobar"); // Setting up as It.IsAny<int>
    var classToTest2 = new ClassToTest(mock2.Object);
    Assert.Equal("foobar", classToTest2.SomeMethod()); // This throws with null != "foobar"
}

A similar issue was reported on #444 but could not be reproduced.

@Kiechlus Kiechlus changed the title Setup fails in a constellation with nullable ints Setup return value fails in a constellation with nullable ints Dec 7, 2021
@stakx stakx added the bug label May 12, 2022
@stakx

This comment was marked as outdated.

@stakx stakx removed the bug label May 13, 2022
@stakx
Copy link
Contributor

stakx commented May 13, 2022

Actually, I take that back, this is a (admittedly subtle) user mistake, not a bug in Moq: You've set up MockedMethod to return "foobar" when it gets called with any int value... but the method gets called with null, which is not a valid int value; therefore the call to MockedMethod quite correctly doesn't match your setup. And since you haven't set mock2.CallBase = true, the base implementation (which would return "test") gets ignored, too.

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

No branches or pull requests

2 participants