コード例 #1
0
        public void AddItem_GivenANewItem_AddsToDatabase()
        {
            // arrange
            OrderItem item = new OrderItem()
            {
                ID = 23
            };
            Mock <DbSet <OrderItem> > mockSet     = new Mock <DbSet <OrderItem> >();
            Mock <OrderDb>            mockContext = new Mock <OrderDb>();

            mockContext.Setup(c => c.OrderItems).Returns(mockSet.Object);
            OrderItemRepository sut = new OrderItemRepository(mockContext.Object);

            // act
            sut.AddItem(item);

            // assert
            mockSet.Verify(s => s.Add(It.Is <OrderItem>(oi => oi.ID == 23)), Times.Once());
            mockContext.Verify(c => c.SaveChanges(), Times.Once());
        }