public async void Setup() { var dbContext = new OrderDbContext { Orders = Aef.FakeDbSet(orders) }; placedAtDate = DateTime.UtcNow; orderId = new Guid("DE81F6B5-7F29-4AE7-A72B-023F6B58DE72"); var placeOrderCommand = new PlaceOrderCommand { OrderId = orderId, OrderNumber = 100, PlacedAtDate = placedAtDate }; var context = new TestableMessageHandlerContext(); var orderStorageContextMock = new Mock <IDbContextWrapper <OrderDbContext> >(); orderStorageContextMock.SetupIgnoreArgs(x => x.Get(null)).Returns(dbContext); handler = new PlaceOrderCommandHandler(orderStorageContextMock.Object); await handler.Handle(placeOrderCommand, context); await dbContext.SaveChangesAsync(); }
public PlaceOrderCommandHandlerTests() { _placeOrderCommandHandler = new PlaceOrderCommandHandler( _mediator.Object, _orderRepository.Object, _shippingMethodRepository.Object, _paymentMethodRepository.Object); }
public async Task PlaceOrderCommand_validation_should_fail_with_empty_required_fields() { var handler = new PlaceOrderCommandHandler(_unitOfWork, _currencyConverter); var command = new PlaceOrderCommand(Guid.Empty, Guid.Empty, string.Empty); var result = await handler.Handle(command, CancellationToken.None); result.ValidationResult.IsValid.Should().BeFalse(); result.ValidationResult.Errors.Count.Should().Be(3); }
public async Task Order_has_been_placed_for_customer() { var currency = Currency.CanadianDollar; var productPrice = 12.5; var productQuantity = 10; var customerEmail = "*****@*****.**"; var productMoney = Money.Of(Convert.ToDecimal(productPrice), currency.Code); _currencyConverter.Convert(currency, Money.Of(Convert.ToDecimal(productPrice * productQuantity), currency.Code)) .Returns(productMoney); var customerUniquenessChecker = Substitute.For <ICustomerUniquenessChecker>(); customerUniquenessChecker.IsUserUnique(customerEmail).Returns(true); var customerId = new CustomerId(Guid.NewGuid()); var customer = Customer.CreateNew(customerEmail, "Customer X", customerUniquenessChecker); _customers.GetById(Arg.Any <CustomerId>()).Returns(customer); var product = Product.CreateNew("Product X", productMoney); _products.GetById(Arg.Any <ProductId>()).Returns(product); var productData = new QuoteItemProductData(product.Id, product.Price, productQuantity); var quote = Quote.CreateNew(customerId); quote.AddItem(productData); List <Product> products = new List <Product>() { product }; _quotes.GetById(quote.Id).Returns(quote); _products.GetByIds(Arg.Any <List <ProductId> >()).Returns(products); var placeOrderCommandHandler = new PlaceOrderCommandHandler(_unitOfWork, _currencyConverter); var placeOrderCommand = new PlaceOrderCommand(quote.Id.Value, customerId.Value, currency.Code); var orderResult = await placeOrderCommandHandler.Handle(placeOrderCommand, CancellationToken.None); await _unitOfWork.Received(1).CommitAsync(Arg.Any <CancellationToken>()); orderResult.Should().NotBe(Guid.Empty); }
public async void Setup() { var dbContext = new OrderDbContext() { Orders = Aef.FakeDbSet(orders) }; placedAtDate = DateTime.UtcNow; orderId = new Guid("DE81F6B5-7F29-4AE7-A72B-023F6B58DE72"); var placeOrderCommand = new PlaceOrderCommand { OrderId = orderId, OrderNumber = 100, PlacedAtDate = placedAtDate }; var context = new TestableMessageHandlerContext(); //TODO: Question? How do you test this, since I cannot inject the OrderDbContext into the handler //In the handler there is the context.SynchronizedStorageSession.FromCurrentSession() that returns a new OrderDbContext //In V5 we did new PlaceOrderCommandHandler(OrderDbContext dbContext); and then we could use the FakeDbSet. //Since I could not use Moq to mock the return value of context.SynchronizedStorageSession.FromCurrentSession() since it is an extension method //so I created a OrderStorageContext instead //Then I realized that U guys have your own TestingFramework that I installed so that I could get a TestableMessageHandlerContext //but cannot find a way to set the session.SqlPersistenceSession(); var orderStorageContextMock = new Mock <IDbContextWrapper <OrderDbContext> >(); orderStorageContextMock.SetupIgnoreArgs(x => x.GetDbContext(null)).Returns(dbContext); handler = new PlaceOrderCommandHandler(orderStorageContextMock.Object); try { await handler.Handle(placeOrderCommand, context) .ConfigureAwait(false); await dbContext.SaveChangesAsync() .ConfigureAwait(false); } catch (Exception e) { Console.WriteLine(e); throw; } }