首页 > 解决方案 > 如何最小起订量为空或空异常

问题描述

我正在为 .NET CORE 5 Web API 项目开发 xUnit 自动化。我有一个响应对象,它有一个属性作为异常。我正在使用夹具来创建最小起订量数据。在不需要抛出异常的情况下,如何使用夹具创建空异常或空异常。

在运动中,夹具会产生异常,这对我来说是不正确的。我试图通过 Null 但它抛出异常

查询响应

public class QueryResultSummary
{
    public int RecordsCount { get; set; }
    public bool IsRecordExist { get; set; }
    public bool Error { get; set; }
    public string ExecutionMessage { get; set; }
    public string CustomMessage { get; set; }
    public Exception Exceptions { get; set; }
}

xUnit

public async Task APIMethodX_ShouldReturn_DoesNotExist_When_SiteNotFound()
    {
        //Arrange
        var fixture = new Fixture();
        long startDateTimeUtc = 1626994800000;
        int siteId = 5588;

        fixture.Customize<QueryResultSummary>(c => c
           .With(x => x.RecordsCount, 0)
           .With(x => x.IsRecordExist, false)
           .With(x => x.Error, false)
           .With(x => x.ExecutionMessage, QueryExecutionStatusEnum.DoesNotExist.ToString())
           .With(x => x.Exceptions, null) // need help here
       );

在此处输入图像描述

标签: c#.net-corexunitautofixture

解决方案


使用时省略属性的正确方法Customize是使用Without().

fixture.Customize<QueryResultSummary>(c => c
    .Without(x => x.Exceptions));

如果您需要省略所有自动属性,您可以使用该OmitAutoProperties方法。

fixture.Customize<QueryResultSummary>(c => c
    .OmitAutoProperties());

推荐阅读