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

ProtectedAsMock issues #1162

Closed
tonyhallett opened this issue May 29, 2021 · 6 comments · Fixed by #1185
Closed

ProtectedAsMock issues #1162

tonyhallett opened this issue May 29, 2021 · 6 comments · Fixed by #1185

Comments

@tonyhallett
Copy link
Contributor

Does not work with auto mocking
image
Due to https://github.com/moq/moq4/blob/a6fde8b6d79a7437bf642d115785b97f40779b6a/src/Moq/Protected/ProtectedAsMock.cs#L233

Does not work with virtual properties. ArgumentException
https://github.com/moq/moq4/blob/a6fde8b6d79a7437bf642d115785b97f40779b6a/src/Moq/Protected/ProtectedAsMock.cs#L289

@stakx
Copy link
Contributor

stakx commented May 29, 2021

Could you please post a minimal, complete & verifiable repro demonstrating a use case where this error gets triggered? So that we know why we need a bugfix, and what exactly it is fixing. Thanks.

@tonyhallett
Copy link
Contributor Author

You can use the tests in my pull request that throw without the fix.

@tonyhallett
Copy link
Contributor Author

@stakx repro provided. These tests fail currently. Fixed with #1163

public class ProtectedAsMockFixture
{
	private Mock<Foo> mock;
	private IProtectedAsMock<Foo, Fooish> protectedMock;

	public ProtectedAsMockFixture()
	{
		this.mock = new Mock<Foo>();
		this.protectedMock = this.mock.Protected().As<Fooish>();
	}

        [Fact]
        public void SetUpGet_can_automock()
        {
	        this.protectedMock.SetupGet(m => m.Nested.Value).Returns(42);

	        var actual = mock.Object.GetNested().Value;

	        Assert.Equal(42, actual);
        }

       [Fact] 
       void SetupGet_can_setup_virtual_property()
       {
	       this.protectedMock.SetupGet(m => m.Virtual).Returns(42);

	       var actual = mock.Object.GetVirtual();

	       Assert.Equal(42, actual);
        }
}

public interface INested
{
	int Value { get; }
}

public abstract class Foo
{
	protected abstract INested Nested { get; set; }

	public INested GetNested()
	{
		return Nested;
	}

	private int virtualProperty;
	public virtual int Virtual
	{
		protected get
		{
			return virtualProperty;
		}
		set
		{
			virtualProperty = value;
		}

	}

	public int GetVirtual()
	{
		return Virtual;
	}
}

public interface Fooish
{
	INested Nested { get; set; }
	int Virtual { get; set; }
}

@tonyhallett
Copy link
Contributor Author

Also fails for methods.

Adding method to INested
``
public interface INested
{
int Value { get; }
int Method(int value);
}

// This fails

[Fact]
public void Setup_can_automock()
{
this.protectedMock.Setup(m => m.Nested.Method(1)).Returns(123);
Assert.Equal(123, mock.Object.GetNested().Method(1));
}
``

Fixed with pull request

@stakx
Copy link
Contributor

stakx commented Jul 19, 2021

@tonyhallett, my apologies if I appear a little dense regarding your issues and PRs. I simply haven't yet found the time to study them all in detail; if I had, perhaps I wouldn't have been quite as confused with my review comments. It's slowly coming together now. Thanks for being patient. :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants