static void Main(string[] args) { AutoMapperConfig.Initialize(); while (true) { Console.WriteLine("Press 1 for cheese, 2 for pepperoni. x to exit"); var result = Console.ReadLine(); if (result == "x") { return; } try { var service = new PizzaOrderService(); var task = service.OrderMenuItemAsync(int.Parse(result)); task.Wait(); var orderId = task.Result; Console.WriteLine("Your order number is " + orderId); } catch (Exception ex) { Console.WriteLine(ex); } } }
public async Task <IHttpActionResult> Order(int menuItem) { // this is painful, // but we can swap out concrete implementation // unfortunately, this is not DRY var loggingService = new WebApiLoggingService(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); try { var orderId = await service.OrderMenuItemAsync(menuItem); return(Created("http://mypizza.com/orders/" + orderId, orderId)); } catch (Exception ex) { loggingService.LogError(ex.Message); return(BadRequest("Oops, no pizza for you.")); } } }
public async Task PizzaOrderServiceOrderFakeTest() { // mock new Mock<ComplianceDbContext>(); var loggingService = new FakeLoggingService(); //db context requires a bit more effort to fake so let's abandon for now Assert.Inconclusive(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); var orderId = await service.OrderMenuItemAsync(1); Assert.IsTrue(orderId != 0); } }
public async Task PizzaOrderServiceOrderTest() { // this is still an integration test // dependent on credit card service // dependent on database // dependent on SMTP server // dependent on RabbitMQ var loggingService = new LoggingService(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); var orderId = await service.OrderMenuItemAsync(1); Assert.IsTrue(orderId != 0); } }
public async Task PizzaOrderServiceOrderShouldFailIfPaymentFailsTest() { var loggingService = new Mock <ILoggingService>(); var queueService = new Mock <IStoreQueueService>(); var notificationService = new Mock <IOrderNotificationService>(); var paymentService = new Mock <IPaymentService>(); paymentService.Setup(x => x.Process(It.IsAny <PizzaOrderModel>())).Returns(default(decimal?)); var context = new Mock <MyPizzaDbContext>(); var mockDbSet = TestHelper.MockDbSet(new List <PizzaOrder>()); context.Setup(x => x.PizzaOrders).Returns(mockDbSet.Object); var service = new PizzaOrderService( context.Object, paymentService.Object, queueService.Object, notificationService.Object, loggingService.Object); var expectedException = false; try { await service.OrderMenuItemAsync(1); } catch (Exception ex) { expectedException = ex.Message.Contains("Payment failed"); } Assert.IsTrue(expectedException); }
public async Task PizzaOrderServiceOrderTest() { var loggingService = new Mock <ILoggingService>(); var queueService = new Mock <IStoreQueueService>(); var notificationService = new Mock <IOrderNotificationService>(); var paymentService = new Mock <IPaymentService>(); paymentService.Setup(x => x.Process(It.IsAny <PizzaOrderModel>())).Returns(42); var context = new Mock <MyPizzaDbContext>(); var mockDbSet = TestHelper.MockDbSet(new List <PizzaOrder>()); context.Setup(x => x.PizzaOrders).Returns(mockDbSet.Object); var service = new PizzaOrderService( context.Object, paymentService.Object, queueService.Object, notificationService.Object, loggingService.Object); await service.OrderMenuItemAsync(1); paymentService.Verify(x => x.Process(It.IsAny <PizzaOrderModel>()), Times.Once); queueService.Verify(x => x.Send(It.IsAny <PizzaOrderModel>()), Times.Once); notificationService.Verify(x => x.Send(It.IsAny <PizzaOrderModel>()), Times.Once); loggingService.Verify(x => x.LogError(It.IsAny <string>()), Times.Never); }
public async Task PizzaOrderServiceOrderTest() { // this test is brittle; often fails when a dependency has a transient fault // accidentally sent emails out one time // accidentally charged a credit card // this is an integration test // dependent on credit card service // dependent on database // dependent on SMTP server // dependent on RabbitMQ var service = new PizzaOrderService(); var orderId = await service.OrderMenuItemAsync(1); Assert.IsTrue(orderId != 0); }
public async Task <IHttpActionResult> Order(int menuItem) { var service = new PizzaOrderService(); try { var orderId = await service.OrderMenuItemAsync(menuItem); return(Created("http://mypizza.com/orders/" + orderId, orderId)); } catch (Exception ex) { //todo: log error with an exception filter return(BadRequest("Oops, no pizza for you.")); } }
static void Main(string[] args) { AutoMapperConfig.Initialize(); while (true) { Console.WriteLine("Press 1 for cheese, 2 for pepperoni. x to exit"); var result = Console.ReadLine(); if (result == "x") { return; } try { // this is painful, // but we can swap out concrete implementation var loggingService = new ConsoleLoggingService(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); var task = service.OrderMenuItemAsync(int.Parse(result)); task.Wait(); var orderId = task.Result; Console.WriteLine("Your order number is " + orderId); } } catch (Exception ex) { Console.WriteLine(ex); } } }
public async Task PizzaOrderServiceOrderTest() { var loggingService = new LoggingService(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); var orderId = await service.OrderMenuItemAsync(1); Assert.IsTrue(orderId != 0); } }