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

Moq.SetupGet doesn't work with Mock.Of<> #526

Closed
ghost opened this issue Nov 21, 2017 · 2 comments
Closed

Moq.SetupGet doesn't work with Mock.Of<> #526

ghost opened this issue Nov 21, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Nov 21, 2017

When a mock is created with new Mock<>, a getter can be modified with SetupGet.
When a mock is created with Mock.Of<>, SetupGet doesn't seem to work.

Code sample :

    public static void Test()
    {
        var mockConnection1 = new Mock<EntityConnection>();
        var connection1 = mockConnection1.Object;
        mockConnection1.SetupGet(c => c.ConnectionString).Returns("_");

        var connection2 = Mock.Of<EntityConnection>();
        var mockConnection2 = Mock.Get(connection2);
        mockConnection2.SetupGet(c => c.ConnectionString).Returns("_");

        Assert.Equal("_", connection1.ConnectionString); // OK
        Assert.Equal("_", connection2.ConnectionString); // "_" != null
    }

I'm using nuget 4.7.145

@stakx
Copy link
Contributor

stakx commented Nov 21, 2017

I cannot reproduce that issue given Moq 4.7.145, your test code, and a straightforward definition for EntityConnection such as:

public interface EntityConnection
{
    string ConnectionString { get; set; }
}

Could you please update your repro code to be minimally complete (i.e. include the relevant parts of the type that you're mocking), and make sure it reproduces the issue you're describing?

@stakx
Copy link
Contributor

stakx commented Nov 21, 2017

I could reproduce the problem with the following type definition:

public abstract class DbConnection
{
    public abstract string ConnectionString { get; set; }
}

public class EntityConnection : DbConnection
{
    public override string ConnectionString { get; set; }
}

I can confirm that this issue has already been fixed in the develop branch (most likely thanks to #509), so your scenario should work just fine with the upcoming version of Moq (≥4.8.0).

@stakx stakx closed this as completed Nov 21, 2017
stakx added a commit to stakx/moq that referenced this issue Dec 30, 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

1 participant