Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to pass number of times invocation is met as parameter to a unit test class method?

I have a unit test class method that currently takes 1 parameter but I want to extend it to receive 2 parameters with the latter being the number of times invocation is met on a mock object.
What I currently have is something like this, which doesn’t compile successfully due to errors

[Theory]
[InlineData("", Times.Never)]
[InlineData("test", Times.Once)]
public async void PostAsync_SendAsync_VerifyOnce(string id, Times outcome)
{
    var mockClients = new Mock<IHubClients>();
...
...
...
    mockClients.Verify(clients => clients.Client(id), outcome);
}

Is it possible to achieve something like this? So in theory both tests here should pass, the first will never be invocated as the first parameter is blank and the second test will be invocated once as the parameter is valid.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can achieve this using the Times.Exactly method:

[Theory]
[InlineData("", 0)]
[InlineData("test", 1)]
public async void PostAsync_SendAsync_VerifyOnce(string id, int expectedCalls)
{
    var mockClients = new Mock<IHubClients>();
...
...
...
    mockClients.Verify(clients => clients.Client(id), Times.Exactly(expectedCalls));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading