Exemplo n.º 1
0
        public void PlaceOrderWithPromotionOk()
        {
            //SETUP
            var mockDbA   = new MockPlaceOrderDbAccess(false, 999);
            var service   = new PlaceOrderAction(mockDbA);
            var lineItems = new List <OrderLineItem>
            {
                new OrderLineItem {
                    BookId = 1, NumBooks = 1
                },
                new OrderLineItem {
                    BookId = 2, NumBooks = 1
                },
            };
            var userId = Guid.NewGuid();

            //ATTEMPT
            var result = service.Action(new PlaceOrderInDto(true, userId, lineItems.ToImmutableList()));

            //VERIFY
            service.Errors.Any().ShouldEqual(false);

            var orderLineItems = mockDbA.AddedOrder.LineItems.ToList();

            orderLineItems.First().BookPrice.ShouldEqual(999);
            orderLineItems.Last().BookPrice.ShouldEqual(2);
        }
Exemplo n.º 2
0
        public void MissingBookError()
        {
            //SETUP
            var mockDbA   = new MockPlaceOrderDbAccess();
            var service   = new PlaceOrderAction(mockDbA);
            var lineItems = new List <OrderLineItem>
            {
                new OrderLineItem {
                    BookId = 1, NumBooks = 4
                },
                new OrderLineItem {
                    BookId = 1000, NumBooks = 5
                },
                new OrderLineItem {
                    BookId = 3, NumBooks = 6
                }
            };
            var userId = Guid.NewGuid();

            //ATTEMPT
            var ex = Assert.Throws <InvalidOperationException>(
                () => service.Action(new PlaceOrderInDto(true, userId, lineItems.ToImmutableList())));

            //VERIFY
            ex.Message.ShouldEqual("An order failed because book, id = 1000 was missing.");
        }
Exemplo n.º 3
0
        public void TestMockDefaultBooks()
        {
            //SETUP

            //ATTEMPT
            var mock = new MockPlaceOrderDbAccess();

            //VERIFY
            mock.Books.Count.ShouldEqual(10);
            mock.Books.ForEach(b => b.Promotion.ShouldBeNull());
            mock.Books.ForEach(b => b.PublishedOn.Year.ShouldEqual(EfTestData.DummyBookStartDate.Year));
        }
Exemplo n.º 4
0
        public void TestMockPromotionOnFirstBooks()
        {
            //SETUP

            //ATTEMPT
            var mock = new MockPlaceOrderDbAccess(false, 100);

            //VERIFY
            mock.Books.Count.ShouldEqual(10);
            mock.Books.First().Promotion.ShouldNotBeNull();
            mock.Books.First().Promotion.NewPrice.ShouldEqual(100);
            mock.Books.Skip(1).ToList().ForEach(b => b.Promotion.ShouldBeNull());
        }
Exemplo n.º 5
0
        public void NotAcceptTsAndCs()
        {
            //SETUP
            var mockDbA = new MockPlaceOrderDbAccess(true);
            var service = new PlaceOrderAction(mockDbA);
            var userId  = Guid.NewGuid();

            //ATTEMPT
            service.Action(new PlaceOrderInDto(false, userId, null));

            //VERIFY
            service.Errors.Any().ShouldEqual(true);
            service.Errors.Count.ShouldEqual(1);
            service.Errors.First().ErrorMessage.ShouldEqual("You must accept the T&Cs to place an order.");
        }
Exemplo n.º 6
0
        public void TestMockIncreasingYearBooks()
        {
            //SETUP

            //ATTEMPT
            var mock = new MockPlaceOrderDbAccess(true);

            //VERIFY
            var expectedYear = EfTestData.DummyBookStartDate.Year;

            foreach (var book in mock.Books)
            {
                book.PublishedOn.Year.ShouldEqual(expectedYear++);
            }
            mock.Books.Last().PublishedOn.Year.ShouldEqual(DateTime.Today.Year + 1);
        }
Exemplo n.º 7
0
        public void BookNotForSale()
        {
            //SETUP
            var mockDbA   = new MockPlaceOrderDbAccess(false, -1);
            var service   = new PlaceOrderAction(mockDbA);
            var lineItems = new List <OrderLineItem>
            {
                new OrderLineItem {
                    BookId = 1, NumBooks = 1
                },
            };
            var userId = Guid.NewGuid();

            //ATTEMPT
            service.Action(new PlaceOrderInDto(true, userId, lineItems.ToImmutableList()));

            //VERIFY
            service.Errors.Any().ShouldEqual(true);
            service.Errors.Count.ShouldEqual(1);
            service.Errors.First().ErrorMessage.ShouldEqual("Sorry, the book 'Book0000 Title' is not for sale.");
        }
Exemplo n.º 8
0
        public void ExampleOfMockingOk()
        {
            //SETUP                                           //#A
            var lineItems = new List <OrderLineItem>          //#A
            {                                                 //#A
                new OrderLineItem {
                    BookId = 1, NumBooks = 4
                },                                                         //#A
            };                                                             //#A
            var userId = Guid.NewGuid();                                   //#A
            var input  = new PlaceOrderInDto(true, userId,                 //#A
                                             lineItems.ToImmutableList()); //#A

            var mockDbA = new MockPlaceOrderDbAccess();                    //#B
            var service = new PlaceOrderAction(mockDbA);                   //#C

            //ATTEMPT
            service.Action(input); //#D

            //VERIFY
            service.Errors.Any().ShouldEqual(false); //#
            mockDbA.AddedOrder.CustomerId            //#F
            .ShouldEqual(userId);                    //#F
        }
Exemplo n.º 9
0
        public void PlaceOrderOk()
        {
            //SETUP
            var mockDbA   = new MockPlaceOrderDbAccess();
            var service   = new PlaceOrderAction(mockDbA);
            var lineItems = new List <OrderLineItem>
            {
                new OrderLineItem {
                    BookId = 1, NumBooks = 4
                },
                new OrderLineItem {
                    BookId = 2, NumBooks = 5
                },
                new OrderLineItem {
                    BookId = 3, NumBooks = 6
                }
            };
            var userId = Guid.NewGuid();

            //ATTEMPT
            var result = service.Action(new PlaceOrderInDto(true, userId, lineItems.ToImmutableList()));

            //VERIFY
            service.Errors.Any().ShouldEqual(false);
            mockDbA.AddedOrder.CustomerId.ShouldEqual(userId);
            mockDbA.AddedOrder.DateOrderedUtc.Subtract(DateTime.UtcNow).TotalSeconds.ShouldBeInRange(-1, 0);
            mockDbA.AddedOrder.LineItems.Count.ShouldEqual(lineItems.Count);
            var orderLineItems = mockDbA.AddedOrder.LineItems.ToImmutableList();

            for (int i = 0; i < lineItems.Count; i++)
            {
                orderLineItems[i].LineNum.ShouldEqual((byte)(i + 1));
                orderLineItems[i].ChosenBook.BookId.ShouldEqual(lineItems[i].BookId);
                orderLineItems[i].NumBooks.ShouldEqual(lineItems[i].NumBooks);
            }
        }