单元测试,使用Moq验证异常消息

时间:2018-08-29 13:23:27

标签: c# nunit moq

因此,我已经进行了此测试。看起来像这样:

[Test]
public async Task LogErrorAndReturnSuccessIfNoLines()
{
    // Assemble
    const string orderNumber = "00000000";
    const string lineNumber = "001";
    var model = new OCS { Request = new strOCS { OrderLineID = $"{orderNumber}/{lineNumber}" } }; // NB: 12 characters means carpet
    var services = WmsContext.GivenServices();

    services.MockConfig.Setup(x => x.Live).Returns(false);
    services.MockOrderProvider.Setup(x => x.GetAsync(orderNumber, null, "Lines.Delivery")).Returns(Task.FromResult(services.WhenGetOrder()));

    var wmsProvider = services.WhenCreateWmsProvider();

    // Act
    var response = await wmsProvider.ReceiveOrderCompletedAsync(model);

    // Assert
    response.Should().Be("success");
    services.MockLogProvider.Verify(x => x.LogExceptionAsync(new Exception("Order doesn't exist on ERP"), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()));
}

由于此行而失败:

services.MockLogProvider.Verify(x => x.LogExceptionAsync(new Exception("Order doesn't exist on ERP"), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()));

如果我将其更改为此:

services.MockLogProvider.Verify(x => x.LogExceptionAsync(It.IsAny<Exception>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()));

通过。但是我想确保发送的消息完全是 ERP上不存在订单。 如果失败,它会说:

  

消息:Moq.MockException:   预期至少对模拟调用一次,但从未执行过:x => x.LogExceptionAsync(System.Exception:ERP,It.IsAny(),It.IsAny(),It.IsAny>( ),It.IsAny(),It.IsAny(),It.IsAny())   未配置任何设置。

它还指出执行的调用是:

  

ILogProvider.LogTrace(“已收到OCS”,...

  

ILogProvider.LogExceptionAsync(System.Exception:ERP上不存在订单,...

如您所见,第二个是我所追求的。 有人可以看到我在做什么错吗?


这是我的 WmsContext ,因此您可以看到Mocks的创建方式:

public class WmsContext
{
    public readonly Mock<ICormarConfig> MockConfig;
    public readonly Mock<IComplaintsProvider> MockComplaintsProvider;
    public readonly Mock<IDistributionProvider> MockDistributionProvider;
    public readonly Mock<IListItemProvider> MockListItemProvider;
    public readonly Mock<ILogProvider> MockLogProvider;
    public readonly Mock<IMessageProvider> MockMessageProvider;
    public readonly Mock<IOrderLineProvider> MockOrderLineProvider;
    public readonly Mock<IOrderProvider> MockOrderProvider;
    public readonly Mock<IStockProvider> MockStockProvider;
    public readonly Mock<IProofOfDeliveryService> MockProofOfDeliveryService;

    private readonly Lazy<IComplaintsProvider> _lazyComplaintsProvider;
    private readonly Lazy<IDistributionProvider> _lazyDistributionProvider;
    private readonly Lazy<IListItemProvider> _lazyListItemProvider;
    private readonly Lazy<ILogProvider> _lazyLogProvider;
    private readonly Lazy<IMessageProvider> _lazyMessageProvider;
    private readonly Lazy<IOrderLineProvider> _lazyOrderLineProvider;
    private readonly Lazy<IOrderProvider> _lazyOrderProvider;
    private readonly Lazy<IStockProvider> _lazyStockProvider;
    private readonly Lazy<IProofOfDeliveryService> _lazyProofOfDeliveryService;

    private WmsContext()
    {
        MockConfig = new Mock<ICormarConfig>();
        MockComplaintsProvider = new Mock<IComplaintsProvider>();
        MockDistributionProvider = new Mock<IDistributionProvider>();
        MockListItemProvider = new Mock<IListItemProvider>();
        MockLogProvider = new Mock<ILogProvider>();
        MockMessageProvider = new Mock<IMessageProvider>();
        MockOrderLineProvider = new Mock<IOrderLineProvider>();
        MockOrderProvider = new Mock<IOrderProvider>();
        MockStockProvider = new Mock<IStockProvider>();
        MockProofOfDeliveryService = new Mock<IProofOfDeliveryService>();

        _lazyComplaintsProvider = new Lazy<IComplaintsProvider>(() => MockComplaintsProvider.Object);
        _lazyDistributionProvider = new Lazy<IDistributionProvider>(() => MockDistributionProvider.Object);
        _lazyListItemProvider = new Lazy<IListItemProvider>(() => MockListItemProvider.Object);
        _lazyLogProvider = new Lazy<ILogProvider>(() => MockLogProvider.Object);
        _lazyMessageProvider = new Lazy<IMessageProvider>(() => MockMessageProvider.Object);
        _lazyOrderLineProvider = new Lazy<IOrderLineProvider>(() => MockOrderLineProvider.Object);
        _lazyOrderProvider = new Lazy<IOrderProvider>(() => MockOrderProvider.Object);
        _lazyStockProvider = new Lazy<IStockProvider>(() => MockStockProvider.Object);
        _lazyProofOfDeliveryService = new Lazy<IProofOfDeliveryService>(() => MockProofOfDeliveryService.Object);
    }

    public static WmsContext GivenServices() => new WmsContext();

    public IWmsProvider WhenCreateWmsProvider() =>
        new WmsProvider(
            MockConfig.Object, 
            _lazyDistributionProvider, 
            _lazyLogProvider, 
            _lazyComplaintsProvider, 
            _lazyStockProvider, 
            _lazyProofOfDeliveryService, 
            _lazyMessageProvider, 
            _lazyOrderProvider, 
            _lazyOrderLineProvider, 
            _lazyListItemProvider
            );

    public OrderViewModel WhenGetOrder(params string[] lineNumbers)
    {
        var lines = lineNumbers.Select(lineNumber => new OrderLineViewModel {Id = lineNumber}).ToList();

        return new OrderViewModel
        {
            Lines = lines
        };
    }
}

1 个答案:

答案 0 :(得分:3)

它不匹配,因为您比较的两个异常不是Exception的相同实例。

您可以通过传递这样的lambda表达式来检查异常的消息部分是否是您想要的

services.MockLogProvider.Verify(x => x.LogExceptionAsync(
   It.Is<Exception>(e => e.Message == "Order doesn't exist on ERP"), 
   It.IsAny<string>(), 
   It.IsAny<string>(), 
   It.IsAny<Dictionary<string, string>>(), 
   It.IsAny<string>(), 
   It.IsAny<string>(), 
   It.IsAny<int>()));