3

I am writing a project that takes a JSON file to deserialize it and Build the data and process them then save it to the database. Now I want to write a unit test using Mock for this class but I don't know how to do this because in this method I only equalize the field in my DTO and database

this is my order 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; }

  }

This is my order builder class

 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
                };
            //};
        }


    }
Nazanin
  • 217
  • 1
  • 6
  • 14

2 Answers2

1

Since you are using entity framework, I think this article will help you: https://learn.microsoft.com/en-us/ef/ef6/fundamentals/testing/mocking

Create your own test doubles – This approach involves writing your own in-memory implementation of your context and DbSets. This gives you a lot of control over how the classes behave but can involve writing and owning a reasonable amount of code.

Use a mocking framework to create test doubles – Using a mocking framework (such as Moq) you can have the in-memory implementations of your context and sets created dynamically at runtime for you.

So with the first way you can simulate a db call with specific data and make sure the result returns successfully.

These tests, while not securing business errors protect your code from missing null checks and runtime errors like that.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
1

UPDATED

First thing to note is that a static class can't be unit tested. So the method to be tested shall be modified as such below shown. Also, the dbcontext i.e. IPracticeEntities4 interface which is implemented in the PracticeEntities4 class need to be injected so that it can be mocked.

 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
            };
        };
    }
}

Now the above method can be united tested.

For unit test, Please check the sample here: Mocking EF DbContext with Moq

Added

Please check this code: github

Ajeet Kumar
  • 687
  • 6
  • 12