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

Getting value of property through read-only interface returns default #923

Closed
Faibbus opened this issue Sep 6, 2019 · 1 comment
Closed

Comments

@Faibbus
Copy link

Faibbus commented Sep 6, 2019

I have an interface A with a read-only property P, and a derived interface B
that shadows P as read/write. When I mock using interface B to write P,
and pass that mocked object to a service expecting interface A, P “becomes” null.

I observed this behaviour on both v4.8.2 and v4.13.0.

using Moq;
using NUnit.Framework;
public interface IReadOnly
{
	int Value { get; }
}
public interface IReadWrite : IReadOnly
{
	new int Value { get; set; }
}

public class Tester
{
	[Test]
	public void TestMock()
	{
		var readWrite = new Mock<IReadWrite>();
		readWrite.SetupAllProperties();
		readWrite.Object.Value = 42;
		IReadOnly readOnly = readWrite.Object;
		Assert.AreEqual(readWrite.Object.Value, readOnly.Value);
	}
}
@stakx
Copy link
Contributor

stakx commented Sep 6, 2019

Simple answer, IReadOnly.Value and IReadWrite.Value are two distinct properties. You're telling the compiler via the new keyword. So obviously setting one property won't affect the other one.

@stakx stakx closed this as completed Sep 6, 2019
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