title: Unit Testing Remove one Item in my Mock Database
body: <p>I am on learning unit testing and have a question.</p>
  
  <p>I mocked my class ApplicationDbContext with my data.</p>
  
  <p>Now I would like to delete an item from my data. :(</p>
  
  <p>The first Test passed but the second failed it still shows me 20 items</p>
  
  <p>Can anyone help me?</p>
  
  <p><strong>My classe IApplicationDbContext :</strong></p>
  
  <pre><code>    public interface IApplicationDbContext : IDisposable
  {
      IDbSet&lt;OrderingEquipment&gt; OrderingEquipments { get; set; }
      int SaveChanges();
      Task&lt;int&gt; SaveChangesAsync();
  }
  </code></pre>
  
  <p><strong>My classe ApplicationDbContext:</strong></p>
  
  <pre><code>    public class ApplicationDbContext : DbContext, IApplicationDbContext
  {
      public virtual IDbSet&lt;OrderingEquipment&gt; OrderingEquipments { get; set; }
  
      public ApplicationDbContext() : base("DefaultConnection")
      {
      }
  }
  </code></pre>
  
  <p><strong>My classe OrderingEquipment:</strong></p>
  
  <pre><code>    public class OrderingEquipment
  {
      [Key]
      public Guid Guid { get; set; }
      public string Text { get; set; }
      public byte?[] xDEHFile { get; set; }
      public string xDEHFileName { get; set; }
      public DateTime Timestamp { get; set; }
  
      public string ModelToString()
      {
          return string.Format("Guid: {0}, Text: {1}, xDEHFile: {2}, xDEHFileName: {3} Timestamp: {4}", Guid, Text, xDEHFile, xDEHFileName, Timestamp);
      }
  }
  </code></pre>
  
  <p><strong>My Controller Methode:</strong></p>
  
  <pre><code>public void Remove(OrderingEquipment orderingEquipments)
      {
          if (orderingEquipments == null)
          {
              //throw exception oder ein Result?
              throw new ArgumentNullException("orderingEquipmentsCointener is null");
          }
  
          try
          {
              using (db)
              {
                  //orderingEquipments.ForEach(x =&gt; log.Error("Removed: " + x.ModelToString()));
                  var itemToRemove = db.OrderingEquipments.Find(orderingEquipments.Guid);
                  db.OrderingEquipments.Remove(itemToRemove);
                  db.SaveChanges();
              }
          }
          catch (Exception e)
          {
             // orderingEquipments.ForEach(x =&gt; log.Error(x.ModelToString(), e));
          }
      }
  </code></pre>
  
  <p><strong>My Unit Tests</strong></p>
  
  <pre><code>public class OrderingEquipmentControllerTests
  {
  
      private IDbSet&lt;OrderingEquipment&gt; dbSet;
      private IApplicationDbContext dbContext;
      private OrderingEquipmentController controller;
  
      [SetUp]
      public void Initialize()
      {
          // Create test product data
          var orderingEquipments = Builder&lt;OrderingEquipment&gt;.CreateListOfSize(20)
              .All()
              .With(p =&gt; p.Guid = Guid.NewGuid())
              .With(p =&gt; p.Timestamp = DateTime.UtcNow.AddMonths(new Random().Next(1)))
              .With(p =&gt; p.Timestamp = DateTime.UtcNow.AddDays(new Random().Next(2)))
              .TheFirst(5)
              .With(p =&gt; p.Text = "First five")
              .Build()
              .AsQueryable();
  
          dbSet = Substitute.For&lt;IDbSet&lt;OrderingEquipment&gt;&gt;();
          dbSet.Provider.Returns(orderingEquipments.Provider);
          dbSet.Expression.Returns(orderingEquipments.Expression);
          dbSet.ElementType.Returns(orderingEquipments.ElementType);
          dbSet.GetEnumerator().Returns(orderingEquipments.GetEnumerator());
          dbSet.Find(Arg.Any&lt;object[]&gt;()).Returns(callinfo =&gt;
          {
              object[] idValues = callinfo.Arg&lt;object[]&gt;();
              if (idValues != null &amp;&amp; idValues.Length == 1)
              {
                  Guid requestedId = (Guid)idValues[0];
                  return orderingEquipments.FirstOrDefault(p =&gt; p.Guid == requestedId);
              }
  
              return null;
          });
  
          dbContext = Substitute.For&lt;IApplicationDbContext&gt;();
          dbContext.OrderingEquipments.Returns(dbSet);
  
          controller = new OrderingEquipmentController(dbContext);
      }
  
      [Test()]
      public void GetAllOrderingEquipmentsTest()
      {
          var items = controller.GetAllOrderingEquipments();
  
          Assert.That(items.Count, Is.EqualTo(20));
      }
  
      [Test()]
      public void RemoveOneOrderingEquipment()
      {
          var items = controller.GetAllOrderingEquipments();
          controller.Remove(items.First());
  
  
          Assert.That(items.Count, Is.EqualTo(19));
      }
  }
  </code></pre>