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

Interface inheritance chains with read-only and read-write properties return defaults instead of mocked value. #275

Closed
david-banister opened this issue Jul 28, 2016 · 2 comments
Labels

Comments

@david-banister
Copy link

This functionality worked in 4.2.1409 and below, but fail with 4.2.1502 and beyond.

Here is a series of tests that show the specific issue:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

public interface IRoot1
{
    int Value { get; }
}

public interface IRoot2
{
    int Value { get; set; }
}

public interface IDerived1 : IRoot1
{
    int Value { get; set; }
}

public interface IDerived2 : IRoot2
{
    int Value { get; set; }
}

public class Implementation1 : IDerived1
{
    public int Value { get; set; }
}

public class Implementation2 : IDerived1
{
    public int Value { get; set; }
}

[TestClass]
public class UnitTest1
{
    private const int EXPECTED = int.MaxValue;

    [TestMethod]
    public void Root1Test()
    {
        var mock = Mock.Of<IRoot1>(c => c.Value == EXPECTED);
        Assert.AreEqual(EXPECTED, mock.Value);
    }
    [TestMethod]
    public void Derived1Test()
    {
        var mock = Mock.Of<IDerived1>(c => c.Value == EXPECTED);
        Assert.AreEqual(EXPECTED, mock.Value);
    }
    [TestMethod]
    public void Implementation1Test()
    {
        var mock = Mock.Of<Implementation1>(c => c.Value == EXPECTED);
        Assert.AreEqual(EXPECTED, mock.Value);
    }
    [TestMethod]
    public void Root2Test()
    {
        var mock = Mock.Of<IRoot2>(c => c.Value == EXPECTED);
        Assert.AreEqual(EXPECTED, mock.Value);
    }
    [TestMethod]
    public void Derived2Test()
    {
        var mock = Mock.Of<IDerived2>(c => c.Value == EXPECTED);
        Assert.AreEqual(EXPECTED, mock.Value);
    }
    [TestMethod]
    public void Implementation2Test()
    {
        var mock = Mock.Of<Implementation2>(c => c.Value == EXPECTED);
        Assert.AreEqual(EXPECTED, mock.Value);
    }
}

All tests pass except Derived1Test. The implementation class always works, and the root interface always works. The only thing that breaks here is when a derived interface declares a read/write property when the root interface declares the same property as read-only.

@stakx
Copy link
Contributor

stakx commented Jun 20, 2017

Visiting for triage. I'm fairly certain that this is a duplicate of #162.

@stakx
Copy link
Contributor

stakx commented Jun 20, 2017

Fixed in Moq 4.7.58.

@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
Projects
None yet
Development

No branches or pull requests

2 participants