I am trying to write a test that verifies that either Foo
or FooAsync
were called. I don't care which one, but I need to make sure at least one of those methods were called.
Is it possible to get Verify
to do this?
So I have:
public interface IExample
{
void Foo();
Task FooAsync();
}
public class Thing
{
public Thing(IExample example)
{
if (DateTime.Now.Hours > 5)
example.Foo();
else
example.FooAsync().Wait();
}
}
If I try to write a test:
[TestFixture]
public class Test
{
[Test]
public void VerifyFooOrFooAsyncCalled()
{
var mockExample = new Mock<IExample>();
new Thing(mockExample.Object);
//use mockExample to verify either Foo() or FooAsync() was called
//is there a better way to do this then to catch the exception???
try
{
mockExample.Verify(e => e.Foo());
}
catch
{
mockExample.Verify(e => e.FooAsync();
}
}
}
I could try and catch the assertion exception, but that seems like a really odd work around. Is there an extension method for moq that would do this for me? Or is there anyway to get the method invocation count?
Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method.
Verifiable(); 'Setup' mocks a method and 'Returns' specify what the mocked method should return. 'Verifiable' marks this expectation to verified at the end when Verify or VerifyAll is called i.e. whether AddIncomePeriod was called with an object of IncomePeriod and if it returned the same output.
Moq can be used to mock both classes and interfaces.
You can create setups for the methods and add callbacks for them, then use that to set a boolean to test.
e.g. something like:
var mockExample = new Mock<IExample>();
var hasBeenCalled = false;
mockExample.Setup(e => e.Foo()).Callback(() => hasBeenCalled = true);
mockExample.Setup(e => e.FooAsync()).Callback(() => hasBeenCalled = true);
new Thing(mockExample.Object);
Assert.IsTrue(hasBeenCalled);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With