title: Unit test for update method in C# using Mock
body: <p>I am trying to write a unit test for My Processor class I have two problems</p>
  
  <ul>
  <li>I do not know how to test my Methods Only.
  this is my processor</li>
  </ul>
  
  <p><br></p>
  
  <p><strong>OrderProcessor class</strong></p>
  
  <pre><code> public  class OrderProcessor
          { 
       public   void Process(CustomersOrder order)
              {
                 var oldOrder = _repository.GetOldorderId(order.Code.Value);
                  if (oldOrder != 0)
                  {
                      updateOrder(order);
                  }
                  else
                  {
                      SaveOrder(order);
  }
      }
  private  void updateOrder(CustomersOrder order)
              {
  
                  _repository.UpdateOrder(order);
  
              }
  
              private  void SaveOrder(CustomersOrder order)
              {
                  _repository.SaveOrder(order);
              }
          }
      }
  </code></pre>
  
  <p><strong>Repository class</strong></p>
  
  <pre><code>  public  class Repository : IRepository
          {
              private static PracticeEntities4 _context;
  
              public Repository(PracticeEntities4 context)
              {
                  _context = context;
              }
  
              public int GetOldCustomerId( int customerCode)
              {
                  var CuID= _context.Customers.First(e =&gt; e.Code == customerCode);
                  return CuID.Id;
              }
  
  
  
              public int GetOldorderId(int orderCode)
              {
                  var oldOrder = _context.CustomersOrders.FirstOrDefault(e =&gt; e.Code == orderCode);
  
                  return oldOrder.Id;
              }
  
              public void SaveCustomer(Customer customer)
              {
                  _context.Customers.Add(customer);
                  _context.SaveChanges();
              }
  
              public void SaveOrder(CustomersOrder order)
              {
                  _context.CustomersOrders.Add(order);
                  _context.SaveChanges();
              }
  
              public void UpdateOrder(CustomersOrder order)
              {
  
                  _context.CustomersOrders.AddOrUpdate(order);
                  _context.SaveChanges();
              }
  
  
          }
  </code></pre>
  
  <p>and this is My unit test I don't know how to fix it and where is the problem exactly and also I want to test the Methods too.</p>
  
  <p><strong>UnitTests Class</strong></p>
  
  <pre><code>    [TestClass]
         public class OrderProcessorTest
          {
            [ClassInitialize]   
              {...}
              [TestInitialize]
              public void TestInitialize()
              {
               ....
              }
              [TestMethod]
              public void Customer_OrderProcess()
              {
                  //Arange
                Mock&lt;IRepository&gt; mock= new Mock&lt;IRepository&gt;();
  
                  //Act
                  mock.Setup(e =&gt; e.GetOldCustomerId(1001)).Returns(3);
                  mock.Setup(e =&gt; e.GetOldStoreId(200)).Returns(3);       
                  var dtos = OrderDeserializer.Deserialize(path);
                  var dto = dtos.First(e =&gt; e.Code == 300);
                  OrderBuilder builder = new OrderBuilder(mock.Object);
                  builder.OrderBuild(dto);
                  //Asset
                  Assert.AreEqual(0, _orders.Count);
  }
  }
  </code></pre>
  
  <p><strong>Order Builder Class</strong></p>
  
  <pre><code>public class OrderBuilder
      {
          public IRepository _repository { get; set; }
  
          public OrderBuilder(IRepository repository)
          {
              _repository = repository;
          }
  
          public CustomersOrder OrderBuild(OrderDto dto)
          {     
              var oldStoreId = _repository.GetOldStoreId(dto.StoreCode);
              var oldCustomerId = _repository.GetOldCustomerId(dto.CustomerCode);
              return new CustomersOrder()
              {
                  OrderDate = Convert.ToDateTime(dto.OrderDate),
                  OrderStatus = dto.OrderStatus,
                  DeliveryDate = Convert.ToDateTime(dto.DeliveryDate),
                  CustomerId = oldCustomerId,
                  StoreId = oldStoreId,
                  Code = dto.Code
              };
          }
      }
  </code></pre>