コード例 #1
0
        public void GetBasketWhenRepositoryHasBasketWillReturnBasketWithCorrectContents()
        {
            // Fixture setup
            var fixture  = new RepositoryFixture();
            var products = fixture.CreateMany <Extent>().ToList();

            fixture.Register(() =>
            {
                var repositoryStub = new Mock <BasketRepository>();
                repositoryStub.Setup(r => r.GetBasketFor(It.IsAny <IPrincipal>())).Returns(products);
                return(repositoryStub.Object);
            });
            var expectedItems = (from e in products
                                 select new Likeness <Extent, Extent>(e)
                                 .With(d => d.Product).EqualsWhen((s, d) => new Likeness <Product, Product>(s.Product).Equals(d.Product))
                                 .Without(d => d.Total)
                                 .Without(d => d.Updated))
                                .ToList();
            var sut = fixture.CreateAnonymous <BasketService>();
            // Exercise system
            var result = fixture.Get((IPrincipal user) => sut.GetBasketFor(user));

            // Verify outcome
            Assert.True(expectedItems.Cast <object>().SequenceEqual(result.Contents.Cast <object>()));
            // Teardown
        }
コード例 #2
0
        public void GetBasketWillApplyDiscountPolicyOnBasket()
        {
            // Fixture setup
            var fixture = new RepositoryFixture();

            var discountPolicyMock = new Mock <BasketDiscountPolicy>();

            discountPolicyMock.Setup(p => p.Apply(It.IsAny <Basket>())).Returns((Basket b) => b);
            fixture.Register(() => discountPolicyMock.Object);

            fixture.Register(() =>
            {
                var basketRepositoryStub = new Mock <BasketRepository>();
                basketRepositoryStub.Setup(r => r.GetBasketFor(It.IsAny <IPrincipal>())).Returns(fixture.CreateMany <Extent>());
                return(basketRepositoryStub.Object);
            });

            var sut = fixture.CreateAnonymous <BasketService>();
            // Exercise system
            var basket = fixture.Get((IPrincipal user) => sut.GetBasketFor(user));

            // Verify outcome
            discountPolicyMock.Verify(p => p.Apply(basket), "Discount policy not correctly invoked");
            // Teardown
        }
コード例 #3
0
 public void GetFeaturedProductsWillReturnInstance()
 {
     // Fixture setup
     var fixture = new RepositoryFixture();
     var sut = fixture.CreateAnonymous<ProductService>();
     // Exercise system
     IEnumerable<Product> result = fixture.Get((IPrincipal user) => sut.GetFeaturedProducts(user));
     // Verify outcome
     Assert.NotNull(result);
     // Teardown
 }
コード例 #4
0
        public void GetFeaturedProductsWillReturnInstance()
        {
            // Fixture setup
            var fixture = new RepositoryFixture();
            var sut     = fixture.CreateAnonymous <ProductService>();
            // Exercise system
            IEnumerable <Product> result = fixture.Get((IPrincipal user) => sut.GetFeaturedProducts(user));

            // Verify outcome
            Assert.NotNull(result);
            // Teardown
        }
コード例 #5
0
 public void GetBasketWhenRepositoryHasNoBasketWillReturnEmptyBasket()
 {
     // Fixture setup
     var fixture = new RepositoryFixture();
     fixture.Register(() =>
         {
             var repositoryStub = new Mock<BasketRepository>();
             repositoryStub.Setup(r => r.GetBasketFor(It.IsAny<IPrincipal>())).Returns(Enumerable.Empty<Extent>());
             return repositoryStub.Object;
         });
     var sut = fixture.CreateAnonymous<BasketService>();
     // Exercise system
     Basket result = fixture.Get((IPrincipal user) => sut.GetBasketFor(user));
     // Verify outcome
     Assert.False(result.Contents.Any(), "GetBasketFor");
     // Teardown
 }
コード例 #6
0
        public void GetBasketWhenRepositoryHasNoBasketWillReturnEmptyBasket()
        {
            // Fixture setup
            var fixture = new RepositoryFixture();

            fixture.Register(() =>
            {
                var repositoryStub = new Mock <BasketRepository>();
                repositoryStub.Setup(r => r.GetBasketFor(It.IsAny <IPrincipal>())).Returns(Enumerable.Empty <Extent>());
                return(repositoryStub.Object);
            });
            var sut = fixture.CreateAnonymous <BasketService>();
            // Exercise system
            Basket result = fixture.Get((IPrincipal user) => sut.GetBasketFor(user));

            // Verify outcome
            Assert.False(result.Contents.Any(), "GetBasketFor");
            // Teardown
        }
コード例 #7
0
        public void GetBasketWillReturnBasketWithAppliedDiscountPolicy()
        {
            // Fixture setup
            var fixture = new RepositoryFixture();

            var expectedBasket = fixture.CreateAnonymous <Basket>();

            var discountPolicyMock = new Mock <BasketDiscountPolicy>();

            discountPolicyMock.Setup(dp => dp.Apply(It.IsAny <Basket>())).Returns(expectedBasket);
            fixture.Register(() => discountPolicyMock.Object);

            var sut = fixture.CreateAnonymous <BasketService>();
            // Exercise system
            var result = fixture.Get((IPrincipal user) => sut.GetBasketFor(user));

            // Verify outcome
            Assert.Equal <Basket>(expectedBasket, result);
            // Teardown
        }
コード例 #8
0
        public void GetFeaturedProductsWillReturnCorrectProductsForNonPreferredUser()
        {
            // Fixture setup
            var fixture = new RepositoryFixture();
            var featuredProducts = fixture.CreateMany<Product>().ToList();
            fixture.Register(() =>
                {
                    var repositoryStub = new Mock<ProductRepository>();
                    repositoryStub.Setup(r => r.GetFeaturedProducts()).Returns(featuredProducts);
                    return repositoryStub.Object;
                });

            var expectedProducts = (from p in featuredProducts
                                    select new Likeness<Product, Product>(p)).ToList();

            var sut = fixture.CreateAnonymous<ProductService>();
            // Exercise system
            var result = fixture.Get((IPrincipal user) => sut.GetFeaturedProducts(user));
            // Verify outcome
            Assert.True(expectedProducts.Cast<object>().SequenceEqual(result.Cast<object>()));
            // Teardown
        }
コード例 #9
0
        public void GetFeaturedProductsWillReturnCorrectProductsForNonPreferredUser()
        {
            // Fixture setup
            var fixture          = new RepositoryFixture();
            var featuredProducts = fixture.CreateMany <Product>().ToList();

            fixture.Register(() =>
            {
                var repositoryStub = new Mock <ProductRepository>();
                repositoryStub.Setup(r => r.GetFeaturedProducts()).Returns(featuredProducts);
                return(repositoryStub.Object);
            });

            var expectedProducts = (from p in featuredProducts
                                    select new Likeness <Product, Product>(p)).ToList();

            var sut = fixture.CreateAnonymous <ProductService>();
            // Exercise system
            var result = fixture.Get((IPrincipal user) => sut.GetFeaturedProducts(user));

            // Verify outcome
            Assert.True(expectedProducts.Cast <object>().SequenceEqual(result.Cast <object>()));
            // Teardown
        }
コード例 #10
0
 public void GetBasketWhenRepositoryHasBasketWillReturnBasketWithCorrectContents()
 {
     // Fixture setup
     var fixture = new RepositoryFixture();
     var products = fixture.CreateMany<Extent>().ToList();
     fixture.Register(() =>
         {
             var repositoryStub = new Mock<BasketRepository>();
             repositoryStub.Setup(r => r.GetBasketFor(It.IsAny<IPrincipal>())).Returns(products);
             return repositoryStub.Object;
         });
     var expectedItems = (from e in products
                          select new Likeness<Extent, Extent>(e)
                             .With(d => d.Product).EqualsWhen((s, d) => new Likeness<Product, Product>(s.Product).Equals(d.Product))
                             .Without(d => d.Total)
                             .Without(d => d.Updated))
                         .ToList();
     var sut = fixture.CreateAnonymous<BasketService>();
     // Exercise system
     var result = fixture.Get((IPrincipal user) => sut.GetBasketFor(user));
     // Verify outcome
     Assert.True(expectedItems.Cast<object>().SequenceEqual(result.Contents.Cast<object>()));
     // Teardown
 }
コード例 #11
0
        public void GetBasketWillReturnBasketWithAppliedDiscountPolicy()
        {
            // Fixture setup
            var fixture = new RepositoryFixture();

            var expectedBasket = fixture.CreateAnonymous<Basket>();

            var discountPolicyMock = new Mock<BasketDiscountPolicy>();
            discountPolicyMock.Setup(dp => dp.Apply(It.IsAny<Basket>())).Returns(expectedBasket);
            fixture.Register(() => discountPolicyMock.Object);

            var sut = fixture.CreateAnonymous<BasketService>();
            // Exercise system
            var result = fixture.Get((IPrincipal user) => sut.GetBasketFor(user));
            // Verify outcome
            Assert.Equal<Basket>(expectedBasket, result);
            // Teardown
        }
コード例 #12
0
        public void GetBasketWillApplyDiscountPolicyOnBasket()
        {
            // Fixture setup
            var fixture = new RepositoryFixture();

            var discountPolicyMock = new Mock<BasketDiscountPolicy>();
            discountPolicyMock.Setup(p => p.Apply(It.IsAny<Basket>())).Returns((Basket b) => b);
            fixture.Register(() => discountPolicyMock.Object);

            fixture.Register(() =>
                {
                    var basketRepositoryStub = new Mock<BasketRepository>();
                    basketRepositoryStub.Setup(r => r.GetBasketFor(It.IsAny<IPrincipal>())).Returns(fixture.CreateMany<Extent>());
                    return basketRepositoryStub.Object;
                });

            var sut = fixture.CreateAnonymous<BasketService>();
            // Exercise system
            var basket = fixture.Get((IPrincipal user) => sut.GetBasketFor(user));
            // Verify outcome
            discountPolicyMock.Verify(p => p.Apply(basket), "Discount policy not correctly invoked");
            // Teardown
        }