async Task Controller_returns_BadRequest_when_LineItems_Empty() { // Arrange var taxCalculatorMock = new Mock <ITaxCalculator>(); var loggerMock = new Mock <ILogger <TaxCalculatorController> >(); OrderTax orderTax = new() { OrderId = "1", CustomerId = "1", TaxCalculatorId = "TAXJAR", AmountToCollect = 1.50m, FreightTaxable = false, OrderTotalAmount = 16.5m, Shipping = 1.5m, TaxableAmount = 15.0m }; taxCalculatorMock.Setup(tx => tx.GetSalesTax(It.IsAny <Order>())).ReturnsAsync(orderTax); var taxCalculatorProviderMock = new Mock <ITaxCalculatorProvider>(); taxCalculatorProviderMock.Setup(tp => tp.GetTaxCalculator(It.IsAny <string>())).Returns(taxCalculatorMock.Object); var controller = new TaxCalculatorController(taxCalculatorProviderMock.Object, loggerMock.Object); // Order without LineItems var order = new Order { Id = "1", CustomerId = "1", TaxCalculatorId = "TAXJAR", Shipping = 1.5m, AddressFrom = new() { CountryCode = "US", StateCode = "FL", Zip = "33029" }, AddressTo = new() { CountryCode = "US", StateCode = "FL", Zip = "33174" }, LineItems = new() { } }; // Act ActionResult <OrderTax> response = await controller.GetTaxes(order); // Assert BadRequestObjectResult result = response.Result as BadRequestObjectResult; Assert.NotNull(result); Assert.Equal(400, result.StatusCode); Assert.Contains("Orders must contain at least one LineItem", (string)result.Value); } } }
async Task Controller_returns_the_right_SalesTaxForOrder() { // Arrange var taxCalculatorMock = new Mock <ITaxCalculator>(); var loggerMock = new Mock <ILogger <TaxCalculatorController> >(); OrderTax orderTax = new() { OrderId = "1", CustomerId = "1", TaxCalculatorId = "TAXJAR", AmountToCollect = 1.50m, FreightTaxable = false, OrderTotalAmount = 16.5m, Shipping = 1.5m, TaxableAmount = 15.0m }; taxCalculatorMock.Setup(tx => tx.GetSalesTax(It.IsAny <Order>())).ReturnsAsync(orderTax); var taxCalculatorProviderMock = new Mock <ITaxCalculatorProvider>(); taxCalculatorProviderMock.Setup(tp => tp.GetTaxCalculator(It.IsAny <string>())).Returns(taxCalculatorMock.Object); var controller = new TaxCalculatorController(taxCalculatorProviderMock.Object, loggerMock.Object); // Minimal Order To Pass Validation var order = new Order { Id = "1", CustomerId = "1", TaxCalculatorId = "TAXJAR", Shipping = 1.5m, AddressFrom = new() { CountryCode = "US", StateCode = "FL", Zip = "33029" }, AddressTo = new() { CountryCode = "US", StateCode = "FL", Zip = "33174" }, LineItems = new() { new() { Id = "abc", ProductTaxCode = "1111", Quantity = 10, UnitPrice = 1.5m, Discount = 0 } } }; // Act ActionResult <OrderTax> response = await controller.GetTaxes(order); // Assert OkObjectResult result = response.Result as OkObjectResult; Assert.NotNull(result); OrderTax tax = Assert.IsAssignableFrom <OrderTax>(result.Value); Assert.Equal(orderTax.OrderId, tax.OrderId); Assert.Equal(orderTax.CustomerId, tax.CustomerId); Assert.Equal(orderTax.AmountToCollect, tax.AmountToCollect); Assert.Equal(orderTax.FreightTaxable, tax.FreightTaxable); Assert.Equal(orderTax.OrderTotalAmount, tax.OrderTotalAmount); Assert.Equal(orderTax.Shipping, tax.Shipping); Assert.Equal(orderTax.TaxableAmount, tax.TaxableAmount); }