Beispiel #1
0
        public void TestAdd_Items()
        {
            //创建两个商品
            var product1 = new Product(_random.Next(999999999))
            {
                Name = "dotnet", Code = "1", Price = 10
            };

            _productRepository.Add(product1);
            var product2 = new Product(_random.Next(999999999))
            {
                Name = "dotnetcore", Code = "2", Price = 20
            };

            _productRepository.Add(product2);
            _unitOfWork.Commit();
            _unitOfWork.ClearCache();

            //创建订单,添加两个订单明细
            Guid orderId = Guid.NewGuid();
            var  order   = new Order(orderId)
            {
                Name = "Order", Code = "123"
            };

            order.AddItem(product1, 2);
            order.AddItem(product2, 3);
            _orderRepository.Add(order);
            _unitOfWork.Commit();
            _unitOfWork.ClearCache();

            //验证
            var result = _orderRepository.Find().Include(t => t.Items).FirstOrDefault(t => t.Id == orderId);

            Assert.Equal("123", result.Code);
            Assert.Equal(2, result.Items.Count);

            //从外部无法通过导航属性添加订单明细,必须调用AddItem方法
            var item = new OrderItem(Guid.NewGuid(), result);

            item.Booking(product1, 2);
            var items = result.Items.ToList();

            items.Add(item);
            _unitOfWork.Commit();
            _unitOfWork.ClearCache();
            result = _orderRepository.Find().Include(t => t.Items).FirstOrDefault(t => t.Id == orderId);
            Assert.Equal(2, result.Items.Count);
        }