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

Functionality Request. Verify Mock using the Mock Setup as base. #1344

Closed
tlogik opened this issue May 23, 2023 · 2 comments
Closed

Functionality Request. Verify Mock using the Mock Setup as base. #1344

tlogik opened this issue May 23, 2023 · 2 comments

Comments

@tlogik
Copy link

tlogik commented May 23, 2023

Problem statement.

When doing setup of mocks and afterwards verifying you do a lot of repeated code, test code can become somewhat unnecessary bloated with text and it is harder to read.

When setting up a mock you describe your expectations to the setup.
Example:

var mockService = new Mock<ISomeService>();
mockService.Setup( x => x.DoStuff(It.IsAny<string>()).ReturnsAsync("Hello Moq");

mockService.Verify(x => x.DoStuff(It.IsAny<string>()), Times.Once);

I propose that what if the Verify could be done much simpler so i do not have to adjust if i change my parameters for the setup and keeping test code clean and concise.
Often they are exactly the same.

Example.

var mockService = new Mock<ISomeService>();
var doStuffMock = mockService.Setup( x => x.DoStuff(It.IsAny<string>()).ReturnsAsync("Hello Moq").Verifiable();

doStuffMock.Verify(Times.Once);

Since i do a lot of Mocks and usually you need to verify that the setup matches the verification, merging this into a convenience method that will allow us to:

  • Limit the amount of code to write to do a mock verification
  • Keep the test code lean
  • Keep the test code lean and also clear.
@stakx
Copy link
Contributor

stakx commented May 24, 2023

@tlogik, IIRC, this can be done already:

var mockService = new Mock<ISomeService>();
mockService.Setup( x => x.DoStuff(It.IsAny<string>()).ReturnsAsync("Hello Moq").Verifiable();
var doStuff = mockService.Setups.Last();

doStuff.Verify();

Unfortunately, we cannot have Setup, ReturnsAsync, and Verifiable return the setup directly due to how the fluent API has been implemented; changing this might be a major breaking change, so for now you'll have to resort to fetching the setup using mock.Setups.Last() or similar.

(Notice: There's likely a small binary breaking change coming up sometime in the future that will affect the last line of code. This is in order to resolve #1093. The optional recursive parameter will be removed from that Verify method. The above source code would still compile even after that change, but binaries will have to be rebuilt.)

@stakx
Copy link
Contributor

stakx commented Jul 20, 2023

@tlogik, I am going to close this issue since it appears that what you requested can already be done. If I misunderstood something, let me know and we can reopen this issue.

@stakx stakx closed this as completed Jul 20, 2023
@devlooped devlooped locked and limited conversation to collaborators Sep 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants