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

Poor error message mocking a non-virtual Set property #1228

Closed
aufde opened this issue Jan 28, 2022 · 1 comment
Closed

Poor error message mocking a non-virtual Set property #1228

aufde opened this issue Jan 28, 2022 · 1 comment

Comments

@aufde
Copy link

aufde commented Jan 28, 2022

Simple setup here, using Moq 4.16.1:

public class Blah
{
	public string Thing { get; set; }
}

public class BlahWrapper
{
	public virtual Blah Blah { get; set; }
	public string Thing { get => Blah.Thing; set => Blah.Thing = value; }
}

public class TestService
{
	public void Test(BlahWrapper blahWrapper)
	{
		blahWrapper.Thing = "";
	}
}

[Test]
public void ActualTest()
{
	var mock = new Mock<BlahWrapper>();
	mock
		.SetupSet(x => x.Thing = It.IsAny<string>())
		.Verifiable();

	new TestService().Test(mock.Object);
}

BlahWrapper.Thing is not virtual so this fails to setup but throws an unhelpful error, Could not determine the correct positions for all argument matchers (1 in total) used in a call to this method: ServiceRequestServiceTests.BlahWrapper.get_Blah.
Making BlahWrapper.Thing virtual allows the test to pass. In BlahWrapper, changing public string Thing { get => Blah.Thing; set => Blah.Thing = value; } to public string Thing { get; set; } correctly throws an error about it being non-virtual instead of the above, unhelpful error message.

@stakx
Copy link
Contributor

stakx commented Jan 30, 2022

Feel free to submit a PR. You'd quickly discover why this is absolutely non-trivial, as everything non-virtual or sealed is basically invisible to Moq 4. Nothing to be done here, the only options as far as I can tell is switching to Moq 5, or otherwise replace the internal matcher guessing machinery behind SetupSet with one that decompiles the passed lambda's IL bytecode.

@stakx stakx closed this as completed Jan 30, 2022
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