【问题标题】:NUnit testing a controller method that returns a listNUnit 测试返回列表的控制器方法
【发布时间】:2021-08-16 19:31:34
【问题描述】:

我正在为控制器方法编写测试。该方法访问SupplyRepository 中的getPharmacySupply 方法,根据项目要求返回PharmacyMedicineSupply 列表:

[HttpPost("PharmacySupply")]
public Task<List<PharmacyMedicineSupply>> GetPharmacySupply([FromBody] List<MedicineDemand> medDemand)
{
        _log4net.Info("Get Pharmacy Supply API Acessed");
        return _supplyRepo.GetPharmacySupply(medDemand);
}

这些是我的模型:

public class MedicineStock
{
    public string Name { get; set; }
    public string ChemicalComposition  { get; set; }
    public string TargetAilment { get; set; }
    public DateTime DateOfExpiry { get; set; }
    public int NumberOfTabletsInStock { get; set; }
}

public class PharmacyMedicineSupply
{
    public string pharmacyName { get; set; }
    public List<MedicineNameAndSupply> medicineAndSupply { get; set; }
}

public class MedicineNameAndSupply
{
    public string medicineName { get; set; }
    public int supplyCount { get; set; }
}

这是我正在写的测试:

public static List<MedicineDemand> mockMedicineDemand= new List<MedicineDemand>()
{
            new MedicineDemand()
            {
                Medicine = "Medcine1",
                DemandCount = 20
            },
            new MedicineDemand()
            {
                Medicine = "Medcine2",
                DemandCount = 25
            },
            new MedicineDemand()
            {
                Medicine = "Medcine3",
                DemandCount = 30
            },
            new MedicineDemand()
            {
                Medicine = "Medcine4",
                DemandCount = 35
            },
            new MedicineDemand()
            {
                Medicine = "Medcine5",
                DemandCount = 40
            }

[Test, TestCaseSource(nameof(mockMedicineDemand))]
public void Test_GetPharmacySupply(List<MedicineDemand> mockMedicineDemand)
{
        Mock<ISupplyRepo> supplyMock = new Mock<ISupplyRepo>();
        
        SupplyController sc = new SupplyController(supplyMock.Object);
        var result = sc.GetPharmacySupply(mockMedicineDemand) as Task<List<PharmacyMedicineSupply>>;
        var resultList = result.Result;
        Assert.That(4,Is.EqualTo( resultList[0].medicineAndSupply[0].supplyCount));
} 

但是我收到了这个错误

“MedicineSupplyMicroservice.Models.MedicineDemand”类型的对象无法转换为“System.Collections.Generic.List`1[MedicineSupplyMicroservice.Models.MedicineDemand]”类型。

我做错了什么??

【问题讨论】:

    标签: unit-testing .net-core nunit


    【解决方案1】:

    您正在测试的方法GetPharmacySupply 采用单个参数,即ListMedicineDemand 对象。

    另一方面,NUnit 使用 single MedicineDemand 调用它。

    看看你的数据源声明...

    public static List<MedicineDemand> mockMedicineDemand= new List<MedicineDemand>()
    {
       ...
    }
    

    TestCaseDataAttribute 指定一个字段、属性或方法,它为一个或多个 测试用例返回必要的数据。您的源返回 List&lt;MedicineDemand&gt;,因此 NUnit 获取该列表并使用内容调用您的方法五次,每个 MedicineDemand 一次。

    这显然是一个错误,NUnit 可能会更聪明一些,并将您的测试标记为不可运行。但是,对于当前版本的 NUnit,它会推迟检查,直到您的测试实际运行。 (一旦你经常使用TestCaseSource,错误信息就会把你引向问题所在。)

    因此,在这种情况下,您的数据源需要是 List&lt;MedicineDemand&gt; 的列表或其他类型的枚举 - 基本上是列表的列表。

    一种方法是使用List&lt;List&lt;MedicineDemand&gt;&gt;,但我认为这会使代码更加混乱,而不是更少!

    这里有很多选项 - 查看文档以获取 TestCaseSourceAttribute。我个人的偏好是将数据源做成一个方法,如下:

        public static IEnumerable<List<MedicineDemand>> MockMedicineDemands()
        {
            yield return new List<MedicineDemand>
            {
                new MedicineDemand() { Medicine = "Medicine1", DemandCount = 20 },
                new MedicineDemand() { Medicine = "Medicine2", DemandCount = 25 },
                new MedicineDemand() { Medicine = "Medicine3", DemandCount = 30 },
                new MedicineDemand() { Medicine = "Medicine4", DemandCount = 35 },
                new MedicineDemand() { Medicine = "Medicine5", DemandCount = 40 }
            };
    
            // Add more cases here if desired
    

    另外,我相信一段时间后,如果您将列表命名为与单个组件不同的名称,例如mockMedicineDemandList 或@ 987654335@.

    公平警告...我现在不方便编译上面的代码,所以它只是“论坛代码”。错别字等很有可能。如果您发现任何错误,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-27
      • 2017-05-08
      • 1970-01-01
      • 2020-06-07
      • 2017-07-23
      • 1970-01-01
      相关资源
      最近更新 更多