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

Wrong Setup by using String interpolation within It.Is<string> #1439

Open
albmargu opened this issue Nov 6, 2023 · 4 comments
Open

Wrong Setup by using String interpolation within It.Is<string> #1439

albmargu opened this issue Nov 6, 2023 · 4 comments

Comments

@albmargu
Copy link

albmargu commented Nov 6, 2023

I upgraded Moq package from 4.16.1 to 4.20.69
I also use the latest .NET test packages under Visual Studio 2022

The setup for the _mockWebService is:

protected Mock<BaseWebService> _mockWebService;

public async Task SetupTest()
{
	_mockWebService = new Mock<Network.BaseWebService>(MockBehavior.Strict);
	var controllers = new List<string>()
	{
		"User",
		"Configuration",
	};
	foreach (var controller in controllers)
		_mockWebService.Setup(x => x.HttpGet<bool>(It.Is<string>(url => url.Contains($"{controller}/")))).ReturnsAsync(false);
}

This code was working well, but is failing now:

Moq.MockException: Mock<BaseWebService:102>:
This mock failed verification due to the following:

   BaseWebService x => x.HttpGet<bool>(It.Is<string>(url => url.Contains(string.Format("{0}/", controller)))):
   This setup was not matched.

The bypass for me was changing the previous foreach into this:

_mockWebService.Setup(x => x.HttpGet<bool>(It.Is<string>(url => url.Contains("User/", StringComparison.OrdinalIgnoreCase)))).ReturnsAsync(false);
_mockWebService.Setup(x => x.HttpGet<bool>(It.Is<string>(url => url.Contains("Configuration/", StringComparison.OrdinalIgnoreCase)))).ReturnsAsync(false);

Should I avoid string interpolation at all in this case?

@albmargu albmargu added the bug label Nov 6, 2023
@ShirAvneri
Copy link

I noticed you only specify StringComparison.OrdinalIgnoreCase in your bypass.
Have you tried adding it to the foreach as well?

@albmargu
Copy link
Author

albmargu commented Dec 1, 2023

No, I didn't.
I noticed in the Stack Trace that the string interpolation is converted in to String.Format(). I should try to use it and check it

@ShirAvneri
Copy link

No, I didn't. I noticed in the Stack Trace that the string interpolation is converted in to String.Format(). I should try to use it and check it

When I ran it locally, it worked as expected, i.e. for https://some-url.com/user/123, the test failed and for https://some-url.com/User/123, the test passed.

Perhaps the following would be more appropriate:
_mockWebService.Setup(x => x.HttpGet<bool>(It.IsRegex($"{controller}/", RegexOptions.IgnoreCase))).ReturnsAsync(false);

@albmargu
Copy link
Author

albmargu commented Dec 4, 2023

No, I didn't. I noticed in the Stack Trace that the string interpolation is converted in to String.Format(). I should try to use it and check it

When I ran it locally, it worked as expected, i.e. for https://some-url.com/user/123, the test failed and for https://some-url.com/User/123, the test passed.

Perhaps the following would be more appropriate: _mockWebService.Setup(x => x.HttpGet<bool>(It.IsRegex($"{controller}/", RegexOptions.IgnoreCase))).ReturnsAsync(false);

You are right, but it worked as is in the previous release.

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

No branches or pull requests

3 participants