Ejemplo n.º 1
0
        public void GetFeaturedProductsWillReturnCorrectProductsForPreferredCustomer()
        {
            // 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, DiscountedProduct>(p)
                                    .With(d => d.UnitPrice).EqualsWhen((s, d) => s.UnitPrice * .95m == d.UnitPrice)).ToList();

            var sut = fixture.CreateAnonymous <ProductService>();

            var preferredCustomerStub = new Mock <IPrincipal>();

            preferredCustomerStub.Setup(u => u.IsInRole("PreferredCustomer")).Returns(true);
            // Exercise system
            var result = sut.GetFeaturedProducts(preferredCustomerStub.Object);

            // Verify outcome
            Assert.True(expectedProducts.Cast <object>().SequenceEqual(result.Cast <object>()));
            // Teardown
        }
Ejemplo n.º 2
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, DiscountedProduct>(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
        }
        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, DiscountedProduct>(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
        }
        public void GetFeaturedProductsWillReturnCorrectProductsForPreferredCustomer()
        {
            // 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, DiscountedProduct>(p)
                                        .With(d => d.UnitPrice).EqualsWhen((s, d) => s.UnitPrice * .95m == d.UnitPrice)).ToList();

            var sut = fixture.CreateAnonymous<ProductService>();

            var preferredCustomerStub = new Mock<IPrincipal>();
            preferredCustomerStub.Setup(u => u.IsInRole("PreferredCustomer")).Returns(true);
            // Exercise system
            var result = sut.GetFeaturedProducts(preferredCustomerStub.Object);
            // Verify outcome
            Assert.True(expectedProducts.Cast<object>().SequenceEqual(result.Cast<object>()));
            // Teardown
        }