'List <choice>'不包含'ChoiceId'的定义,也没有扩展名...您是否缺少using指令或程序集引用?)

时间:2018-07-05 16:18:39

标签: c# .net unit-testing

标题只是错误的一部分,这是许多错误之一:

  

“列表”不包含“ ChoiceId”的定义,找不到可以接受类型为“列表”的第一个参数的扩展方法“ ChoiceId”(您是否缺少using指令或程序集引用?)

我正在尝试如下所述对RepaymentPlan列表进行单元测试。但是我似乎无法获取该列表中的其余值,因为在这些键下方出现红色下划线错误(我在下面的单元测试中用** **包裹了这些键。我做错了,不确定如何解决。

仍然可以学习编程的新知识,因此希望你们能以正确的方式指导我。非常感谢您的帮助。

这是我所拥有的:

型号:

using System.Collections.Generic;

namespace Project.Models
{
    public class Repayment
    {
      public int Amount { get; set; }
      public string DueDate { get; set; }
      public string RepaymentId { get; set; }
    }

   public class Choice
   {
      public string ChoiceId { get; set; }
      public List<Repayment> Repayments{ get; set; }
   }

   public class RepaymentPlan
   {
      public List<Choice> Choices{ get; set; }
   }
}

存储库:

using System.Collections.Generic;
using System.Linq;
using Project.Models;
using System.Net;
using System.Collections.Specialized;
using System.Text;


namespace Project.Repositories
{
   public class OrderRepository
   {
       private static List<RepaymentPlan> _RepaymentPlan;

       static OrderRepository()
       {
           _RepaymentPlan = new List<RepaymentPlan>();

          _RepaymentPlan.Add(new RepaymentPlan 
          {
             Choices = new List<Choice>
                        {
                            new Choice
                            {
                                ChoiceId = "cho1",
                                Repayments = new List<Repayment>
                                                {
                                                    new Repayment
                                                    {
                                                        Amount = 200,
                                                        DueDate = "2018-06-01"
                                                    }, 
                                                    new Repayment
                                                    {
                                                        Amount = 100,
                                                        DueDate = "2018-08-01",
                                                    }
                                                }
                            }, 
                            new Choice
                            {
                                ChoiceId = "cho2",
                                Repayments = new List<Repayment>
                                                {
                                                    new Repayment
                                                    {
                                                        RepaymentId = "Choice1",
                                                        Amount = 300,
                                                        DueDate = "2018-10-01"
                                                    }, 
                                                    new Repayment
                                                    {
                                                        RepaymentId = "Choice2",
                                                        Amount = 150,
                                                        DueDate = "2018-11-01"
                                                    }, 
                                                }
                            }                                             
                        },
             });
}

*单元测试*

[Test]
public void ListRepaymentPlan()
{
     // Arrange
     var controller = new CaseController();
     var expectedResult = new List<RepaymentPlan>();
     var repaymentPlan = new RepaymentPlan()
     {
          Choices = new List<Choice>
                    {
                        new Choice
                        {
                            ChoiceId = "cho1",
                            Repayments = new List<Repayment>
                                            {
                                                new Repayment
                                                {
                                                    Amount = 200,
                                                    DueDate = "2018-06-01"
                                                }, 
                                                new Repayment
                                                {
                                                    Amount = 100,
                                                    DueDate = "2018-08-01"
                                                }, 
                                            }
                       }, 
                        new Choice
                        {
                             ChoiceId = "cho2",
                             Repayments = new List<Repayment>
                                            {
                                                new Repayment
                                                {
                                                    RepaymentId = "Choice1",
                                                    Amount = 300,
                                                    DueDate = "2018-10-01"
                                                }, 
                                                new Repayment
                                                {
                                                    RepaymentId = "Choice2",
                                                    Amount = 150,
                                                    DueDate = "2018-11-01"
                                                }, 
                                            }
                        }                                             
                    },

            };
            expectedResult.Add(RepaymentPlan );

     // Act
     var actionResult = controller.FindRepaymentPlan("578", "156");
     var result = (List<RepaymentPlan >)actionResult;

     // Assert
     for (var i = 0; i < result.Count; i++)
     {
         Assert.AreEqual(expectedResult[i].Choices , result[i].Choices);
         Assert.AreEqual(expectedResult[i].Choices.**ChoiceId**, result[i].Choices.**ChoiceId**);
         Assert.AreEqual(expectedResult[i].Choices.**ChoiceId**.Repayments, result[i].Choices.**ChoiceId**.Repayments);
         Assert.AreEqual(expectedResult[i].Choices.**Repayments**.Amount, result[i].Choices.**Repayments**.Amount);
         Assert.AreEqual(expectedResult[i].Choices.**Repayments**.DueDate, result[i].Choices.**Repayments**.DueDate);
     }
 }

**更新了单元测试**

        // Assert
        for (var i = 0; i < result.Count; i++)
        {
            CollectionAssert.AreEqual(expectedResult[i].Choices, result[i].Choices);
            CollectionAssert.AreEqual(expectedResult[i].Choices[0].ChoiceId, result[i].Choices[0].ChoiceId);
            CollectionAssert.AreEqual(expectedResult[i].Choices[0].Repayments, result[i].Choices[0].Repayments);
            CollectionAssert.AreEqual(expectedResult[i].Choices, result[i].Choices);
            CollectionAssert.AreEqual(expectedResult[i].Choices[1].ChoiceId, result[i].Choices[1].ChoiceId);
            CollectionAssert.AreEqual(expectedResult[i].Choices[1].Repayments, result[i].Choices[1].Repayments);
        }

1 个答案:

答案 0 :(得分:4)

ChoicesList<>,因此您也需要为其编制索引,如下所示:

expectedResult[i].Choices[0].ChoiceId

但是,您也必须遍历这些内部值。使用CollectionAssert更加容易:

CollectionAssert.AreEqual(expectedResult, result);