Esempio n. 1
0
        public void Add_Order()
        {
            // Arrange
            var orderDataService = new OrderDataService(sourceService.Object, iLoggerFactory.Object.CreateLogger <OrderDataService>());
            var rnd       = new Random();
            var ordersQty = 20;
            var currDate  = DateTime.UtcNow;
            var orders    = Enumerable.Range(1, ordersQty).Select(x => new Order()
            {
                Amount      = rnd.Next(x * 1000) + new Random().Next(x) / 100,
                DateCreated = currDate.AddDays(x)
            }).ToList();

            var itms = new List <DbOrder>();

            var ordersMock = TestHelper.ToDbSetMock(itms);

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

            var ord = new Order()
            {
                Id          = 1,
                Amount      = 77.77M,
                DateCreated = DateTime.UtcNow.AddDays(15)
            }.OrderToDbOrder();

            var badOrder = new Order()
            {
                Id          = Int32.MaxValue,
                Amount      = 1.1M,
                DateCreated = DateTime.UtcNow.AddDays(30)
            }.OrderToDbOrder();

            // Action
            orders.ForEach(o => { orderDataService.Add(o.OrderToDbOrder()).Wait(); });

            // Assert
            ordersMock.Verify(x => x.AddRangeAsync(It.IsAny <DbOrder[]>()), Times.Exactly(orders.Count));
            Assert.AreEqual(orders.Count, itms.Count);
            orders.ForEach(os =>
            {
                Assert.IsTrue(itms.Any(x => x.Amount == os.Amount && x.DateCreated == os.DateCreated));
            });
        }