コード例 #1
0
        public void Remove_Order()
        {
            // Arrange
            var orderDataService = new OrderDataService(sourceService.Object, iLoggerFactory.Object.CreateLogger <OrderDataService>());
            var itms             = new List <DbOrder>()
            {
                new Order()
                {
                    Id          = 753,
                    Amount      = 555M,
                    DateCreated = DateTime.UtcNow
                }.OrderToDbOrder(),
                new Order()
                {
                    Id          = new Random().Next(),
                    Amount      = 666M,
                    DateCreated = DateTime.UtcNow
                }.OrderToDbOrder(),
                new Order()
                {
                    Id          = 1,
                    Amount      = 555M,
                    DateCreated = DateTime.UtcNow
                }.OrderToDbOrder(),
            };


            var ordersMock = TestHelper.ToDbSetMock(itms);

            ordersMock.Setup(x => x.RemoveRange(It.IsAny <DbOrder[]>())).Callback <DbOrder[]>((s) =>
            {
                s.ToList().ForEach(fe =>
                {
                    itms.RemoveAll(x => x.Id == fe.Id);
                });
            });
            sourceService.Setup(x => x.Orders).Returns(ordersMock.Object);
            sourceService.Setup(x => x.Set <DbOrder>()).Returns(ordersMock.Object);

            var ordersCount = itms.Count;

            for (var i = itms.Count - 1; i >= 0; i--)
            {
                var restCount      = itms.Count;
                var orderForDelete = itms[i];
                // Action
                orderDataService.Remove(orderForDelete.Id);

                // Assert
                ordersMock.Verify(x => x.RemoveRange(It.IsAny <DbOrder[]>()), Times.Exactly(ordersCount - i));
                Assert.AreEqual(restCount - 1, itms.Count);
                Assert.IsTrue(itms.TrueForAll(x => x.Id != orderForDelete.Id));
            }
        }