Moq Callback在第一次测试中很好,但是相同的代码在第二次测试中返回null

时间:2015-07-30 08:03:01

标签: c# unit-testing moq

有人知道为什么,在2次测试中使用moq时,我有以下代码:

var magentoChannelMock = new Mock<IMagentoChannel>();

var magentoChannelMock = new Mock<IMagentoChannel>();

magentoChannelMock.Setup(x => x.SalesOrderInvoiceCreate(It.IsAny<string>(), "2", It.IsAny<orderItemIdQty[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
                .Callback<string, string, orderItemIdQty[], string, string, string>((s1, s2, x, s3, s4, s5) => invoicesSent = x);

我在测试类中有2个测试,当我单独运行每个测试时,它们运行正常并且回调对象&#34; invoicesSent&#34;包含期望值。但是当我同时运行两个测试时,运行的第二个测试总是返回null作为回调。

有什么想法吗?

[Test]
public void Invoice_3Line_QtyThatIsInvoicedShouldBe12()
{
    //arrange
    orderItemIdQty[] invoicesSent = null;
    var magentoChannelMock = new Mock<IMagentoChannel>();

    magentoChannelMock.Setup(x => x.SalesOrderInfo(It.IsAny<string>(), It.IsAny<string>()))
                      .Returns(new salesOrderEntity() { items = new salesOrderItemEntity[] { new salesOrderItemEntity() { qty_ordered = "12", item_id = "1", sku = "SKU123" } } });
    magentoChannelMock.Setup(x => x.SalesOrderInvoiceCreate(It.IsAny<string>(), "2", It.IsAny<orderItemIdQty[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
        .Callback<string, string, orderItemIdQty[], string, string, string>((s1, s2, x, s3, s4, s5) => invoicesSent = x);

    TestProjIocContainer.Container().RegisterInstance( magentoChannelMock.Object);

    var magentoApiRepository = TestProjIocContainer.ResolveObjectForTesting<IMagentoApiRepository>();

    var orderLineItem = new OrderLineItem{Quantity = 4, Material = new Material{SKU = "SKU123"},ChannelLineItemReference = "123"};

    var testInvoice = new List<InvoiceLine>
        {
            new InvoiceLine(orderLineItem),
            new InvoiceLine(orderLineItem),
            new InvoiceLine(orderLineItem)
        };

    //act
    magentoApiRepository.CreateOrderInvoice("2", testInvoice);

    //assert
    Assert.AreEqual(testInvoice.Sum(x=>x.Quantity), (int)invoicesSent.Sum(x=>x.qty) );
}



[Test]
public void Invoice_1Line_Qty4()
{
    //arrange
    orderItemIdQty[] invoicesSent = null;
    var magentoChannelMock = new Mock<IMagentoChannel>();

    magentoChannelMock.Setup(x => x.SalesOrderInfo(It.IsAny<string>(), It.IsAny<string>()))
                      .Returns(new salesOrderEntity() { items = new salesOrderItemEntity[] { new salesOrderItemEntity() { qty_ordered = "12", item_id = "1", sku = "SKU123" } } });
    magentoChannelMock.Setup(x => x.SalesOrderInvoiceCreate(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<orderItemIdQty[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
        .Callback<string, string, orderItemIdQty[], string, string, string>((s1, s2, x, s3, s4, s5) => invoicesSent = x);

    TestProjIocContainer.Container().RegisterInstance(typeof(IMagentoChannel), "", magentoChannelMock.Object, new HierarchicalLifetimeManager());

    var magentoApiRepository = TestProjIocContainer.ResolveObjectForTesting<IMagentoApiRepository>();

    var orderLineItem = new OrderLineItem { Quantity = 4, Material = new Material { SKU = "SKU123" }, ChannelLineItemReference = "123" };

    var testInvoice = new List<InvoiceLine>
        {
            new InvoiceLine(orderLineItem)
        };

    //act
    magentoApiRepository.CreateOrderInvoice("1", testInvoice);

    //assert
    Assert.AreEqual(4, (int)invoicesSent.Sum(x => x.qty));
}

0 个答案:

没有答案