使用Moq验证使用不同对象状态的方法调用

时间:2016-09-08 20:30:42

标签: c# testing methods mocking moq

最近,我在一个C#项目中发现了Moq库(4.5.21)非常有趣的行为。以下是我要测试的课程。

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);
    }
}

以下是我的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);
}

我得到以下输出:

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>)

我希望Verify每次Once使用object State时,PSXxx可以调用该方法for。对我做错了什么的想法?

谢谢!

2 个答案:

答案 0 :(得分:3)

设置模拟以使用回调。在回调计数内,调用方法的次数

mock.Setup(a => a.UpdateOrderAsync(It.Is<Order>(o => o.State == "new")))
    .Returns(Task.Factory.StartNew(() => { }))
    .Callback(() => countOrderStateNew++);

这是完整的测试方法

[TestMethod]
public async Task TestMethod1()
{

  var mock = new Mock<IOrderService>();

  Order o1 = new Order() { State = "new" };

  int countOrderStateNew = 0;
  int countOrderStateOpen = 0;

  mock.Setup(a => a.UpdateOrderAsync(It.Is<Order>(o => o.State == "new")))
      .Returns(Task.Factory.StartNew(() => { }))
      .Callback(() => countOrderStateNew++);
  mock.Setup(a => a.UpdateOrderAsync(It.Is<Order>(o => o.State == "open")))
      .Returns(Task.Factory.StartNew(() => { }))
      .Callback(()=> countOrderStateOpen++);

  await new Program().RunAsync(mock.Object);

  Assert.AreEqual(1, countOrderStateNew);
  Assert.AreEqual(1, countOrderStateOpen);


}

答案 1 :(得分:2)

这与this post

非常相似

基本上是因为在第一次验证被调用时,模拟所持有的引用(对于特定的调用)已将其State属性更改为“open”(就在第二次调用之前)。这可以通过将Order类更改为struct来证明,以确保mock捕获对象的副本而不是对对象本身的引用。

编辑: 为了得到完整的答案,以上帖子使用的Expect现已过时,这是一个通过测试:

var mock = new Mock<IOrderService>(MockBehavior.Loose);

var newCalled = false;
var openCalledBeforeNew = false;

mock.Setup(x => x.UpdateOrderAsync(It.Is<Order>(o => o.State == "new"))).Callback(() => newCalled = !openCalledBeforeNew).Returns(Task.FromResult((object)null));
mock.Setup(x => x.UpdateOrderAsync(It.Is<Order>(o => o.State == "open"))).Callback(() => openCalledBeforeNew = newCalled).Returns(Task.FromResult((object)null));

await new Program().RunAsync(mock.Object);

Assert.IsTrue(newCalled);
Assert.IsTrue(openCalledBeforeNew);