C# 为构建器类编写单元测试

C# 为构建器类编写单元测试,c#,unit-testing,mocking,C#,Unit Testing,Mocking,我正在写一个项目,它使用一个JSON文件来反序列化它,构建数据并处理它们,然后将其保存到数据库中。现在我想为这个类编写一个使用Mock的单元测试,但我不知道怎么做,因为在这个方法中,我只均衡DTO和数据库中的字段 这是我点的Dto public class OrderDto { public int Code { get; set; } public int CustomerCode { get; set; } pu

我正在写一个项目,它使用一个JSON文件来反序列化它,构建数据并处理它们,然后将其保存到数据库中。现在我想为这个类编写一个使用Mock的单元测试,但我不知道怎么做,因为在这个方法中,我只均衡DTO和数据库中的字段

这是我点的Dto

  public class OrderDto
  {
            public int Code { get; set; }
            public int CustomerCode { get; set; }
            public int StoreCode { get; set; }
            public string OrderDate { get; set; }
            public string OrderStatus { get; set; }
            public string DeliveryDate { get; set; }

  }
这是我的订单生成器类

 public class OrderBuilder
  {
      static PracticeEntities _context;

      public OrderBuilder(PracticeEntities4 context)
      {
          _context = context;
      }
        public static CustomersOrder OrderBuild(OrderDto dto)
        {
            //using (var context = new PracticeEntities4())
            //{
                var oldStoreId = _context.Stores.FirstOrDefault(e => e.Code == dto.StoreCode).Id;
                var oldCustomerId = _context.Customers.FirstOrDefault(e => e.Code == dto.CustomerCode).Id;
                return new CustomersOrder()
                {
                    OrderDate = Convert.ToDateTime(dto.OrderDate),
                    OrderStatus = dto.OrderStatus,
                    DeliveryDate = Convert.ToDateTime(dto.DeliveryDate),
                    CustomerId = oldCustomerId,
                    StoreId = oldStoreId,
                    Code = dto.Code
                };
            //};
        }


    }

由于您使用的是实体框架,我认为本文将帮助您:

创建您自己的双重测试——这种方法涉及编写您自己的上下文和数据库集的内存实现。这使您能够控制类的行为,但可能需要编写和拥有合理数量的代码

使用模拟框架创建双重测试–使用模拟框架(如Moq),可以在运行时为您动态创建上下文和集合的内存中实现

因此,使用第一种方法,您可以使用特定数据模拟db调用,并确保结果成功返回


这些测试虽然不保护业务错误,但可以保护您的代码免受空检查丢失和类似的运行时错误的影响

更新

首先要注意的是,静态类不能进行单元测试。因此,测试方法应修改如下所示。此外,需要注入在
PracticeEntities4
类中实现的dbcontext,即
iPraccesEntities4
接口,以便对其进行模拟

 public class PracticeEntities4:IPracticeEntities4, DbContext
 {
  ....
 } 

public class ClassMethod2BTested
{  
   IPracticeEntities4 _context; //declare the context
   public ClassMethod2BTested(IPracticeEntities4 context) // inject the context
   {
      _context=context; // set the context to local variable
   }
   public CustomersOrder OrderBuild(OrderDto dto)
   {
        //using (var context = new PracticeEntities4()) // remove this
        {
            var oldStoreId = _context.Stores.FirstOrDefault(e => e.Code == dto.StoreCode).Id;
            var oldCustomerId = _context.Customers.FirstOrDefault(e => e.Code dto.CustomerCode).Id;
            return new CustomersOrder()
            {
                OrderDate = Convert.ToDateTime(dto.OrderDate),
                OrderStatus = dto.OrderStatus,
                DeliveryDate = Convert.ToDateTime(dto.DeliveryDate),
                CustomerId = oldCustomerId,
                StoreId = oldStoreId,
                Code = dto.Code
            };
        };
    }
}
现在可以对上述方法进行联合测试

对于单元测试,请在此处检查样本:

已添加

请检查此代码:

首先要注意的是,静态类不能进行单元测试-它可以进行测试。谢谢你,我现在更改了代码,如何编写单元测试?