Recently I discovered quite interesting behaviour of Moq
library (4.5.21) in one of mine C# projects. Below is the class that I am trying to test.
public class Order
{
public string State { get; set; }
}
public interface IOrderService
{
Task UpdateOrderAsync(Order order);
}
public class Program
{
public async Task RunAsync(IOrderService orderService)
{
var order = new Order();
order.State = "new";
await orderService.UpdateOrderAsync(order);
order.State = "open";
await orderService.UpdateOrderAsync(order);
}
}
Below is my TestClass:
[TestMethod]
public async Task TestMethod()
{
var mock = new Mock<IOrderService>();
await new Program().RunAsync(mock.Object);
mock.Verify(x => x.UpdateOrderAsync(It.Is<Order>(o => o.State == "new")), Times.Once);
mock.Verify(x => x.UpdateOrderAsync(It.Is<Order>(o => o.State == "open")), Times.Once);
}
I get following output:
Moq.MockException:
Expected invocation on the mock once, but was 0 times: x => x.UpdateOrderAsync(It.Is<Order>(o => o.State == "new"))
Configured setups:
x => x.UpdateOrderAsync(It.Is<Order>(o => o.State == "new")), Times.Once
x => x.UpdateOrderAsync(It.Is<Order>(o => o.State == "open")), Times.Once
Performed invocations:
IOrderService.UpdateOrderAsync(Order<State:open>)
IOrderService.UpdateOrderAsync(Order<State:open>)
I'd expect that I can Verify
that the method was called Once
each time with an object
with different State
. Any thoughts on what am I doing wrong?
Thank you!