public void GetById_Success() { // Arrange var expected = new TestEntity { Id = 22, Name = "HelloWorld" }; var mockDbSet = new Mock <DbSet <TestEntity> >(); mockDbSet.Setup(d => d.Find(22)).Returns(expected); var mockContext = new Mock <IContext>(); mockContext.Setup(c => c.Set <TestEntity>()).Returns(mockDbSet.Object); var inMemory = new InMemory <TestEntity>(mockContext.Object); // Act var actual = inMemory.GetById(22); // Assert mockDbSet.Verify(d => d.Find(22), Times.Once); mockContext.Verify(c => c.Set <TestEntity>(), Times.Once); Assert.AreEqual(expected, actual); }
public void GetById_ReturnNull() { // Arrange var mockDbSet = new Mock <DbSet <TestEntity> >(); var mockContext = new Mock <IContext>(); mockContext.Setup(c => c.Set <TestEntity>()).Returns(mockDbSet.Object); var inMemory = new InMemory <TestEntity>(mockContext.Object); // Act var actual = inMemory.GetById(-1); // Assert mockContext.Verify(c => c.Set <TestEntity>(), Times.Once); Assert.IsNull(actual); }