コード例 #1
0
            public async Task RespondsWithCreated_PostNewItemType()
            {
                var itemTypeRepository     = new InMemoryItemTypeRepository();
                var itemTypeReadRepository = new InMemoryItemTypeReadRepository();

                itemTypeRepository.EventOccurred += entity => itemTypeReadRepository.UpdateAsync(new ItemTypeReadModel
                {
                    Id   = entity.Id,
                    Code = entity.Code
                });

                var itemTypeController =
                    new ItemTypeController(itemTypeRepository, itemTypeReadRepository)
                    .BootstrapForTests(urlHelper: new ActionNameOnlyUrlHelper());

                var postRequestDto = new PostRequestDto
                {
                    Code = "fruit"
                };

                var postResponse = (CreatedResult)await itemTypeController.Post(postRequestDto);

                Assert.Equal((int)HttpStatusCode.Created, postResponse.StatusCode);

                var itemTypeDto = (ItemTypeDto)postResponse.Value;

                Assert.Equal(nameof(ItemTypeController.Get), postResponse.Location);

                Assert.Equal("fruit", itemTypeDto.Code);
            }
コード例 #2
0
            public async Task ApplyDiscount_FromCouponOnlyForSelectItemType()
            {
                var itemTypeRepository     = new InMemoryItemTypeRepository();
                var itemTypeReadRepository = new InMemoryItemTypeReadRepository();

                itemTypeRepository.EventOccurred += async entity => await new ItemTypeReadModelHandler(itemTypeReadRepository).Handle(entity.NewEvents.ToList <object>());
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());
                var couponRepository     = new InMemoryCouponRepository();
                var couponReadRepository = new InMemoryCouponReadRepository();

                couponRepository.EventOccurred += async entity =>
                                                  await new CouponReadModelHandler(couponReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemTypeController = new ItemTypeController(itemTypeRepository, itemTypeReadRepository)
                                         .BootstrapForTests();

                var postFruitResult = await itemTypeController.Post(
                    new ShoppingCartApi.Controllers.ItemType.PostRequestDto
                {
                    Code = "fruit"
                });

                var fruitItemTypeDto = (ItemTypeDto)postFruitResult.Value;

                var postVegetableResult = await itemTypeController.Post(
                    new ShoppingCartApi.Controllers.ItemType.PostRequestDto
                {
                    Code = "vegetable"
                });

                var vegetableItemTypeDto = (ItemTypeDto)postVegetableResult.Value;

                var itemController = new ItemController(itemRepository, itemReadRepository)
                                     .BootstrapForTests();

                var postPotatoResult = await itemController.Post(new PostRequestDto
                {
                    Code       = "potato",
                    Price      = 30,
                    ItemTypeId = vegetableItemTypeDto.Id
                });

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var postAppleResult = await itemController.Post(new PostRequestDto
                {
                    Code       = "apple",
                    Price      = 50,
                    ItemTypeId = fruitItemTypeDto.Id
                });

                var appleDto = (ItemDto)postAppleResult.Value;

                var couponController = new CouponController(couponRepository)
                                       .BootstrapForTests();
                var postRequestDto = new ShoppingCartApi.Controllers.Coupon.PostRequestDto
                {
                    Code          = "HALF_OFF_FRUIT",
                    PercentOff    = 50,
                    ForItemTypeId = fruitItemTypeDto.Id
                };
                await couponController.Post(postRequestDto);

                var calculatorController =
                    new CalculatorController(itemRepository, couponReadRepository)
                    .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    CouponCode    = "HALF_OFF_FRUIT",
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 3
                        },
                        new ShoppingItemDto
                        {
                            Id       = appleDto.Id,
                            Quantity = 1
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(115, dto.TotalCost);
            }