public void ApplyWillApplyDefaultProductDiscountForPreferredCustomer() { // Fixture setup var fixture = new RepositoryFixture(); fixture.Register(() => { var customerStub = new Mock<IPrincipal>(); customerStub.Setup(p => p.IsInRole("PreferredCustomer")).Returns(true); return customerStub.Object; }); var basket = fixture.CreateAnonymous<Basket>(); fixture.AddManyTo(basket.Contents); var expectedExtents = (from e in basket.Contents let ep = new DefaultCustomerDiscountPolicy().Apply(e.Product, basket.Owner) select new Likeness<Extent, Extent>(e) .With(d => d.Product).EqualsWhen((s, d) => new Likeness<Product, Product>(ep) .Equals(d.Product)) .Without(d => d.Total) .Without(d => d.Updated)) .ToList(); var sut = fixture.CreateAnonymous<DefaultProductDiscountPolicy>(); // Exercise system var result = sut.Apply(basket); // Verify outcome Assert.True(expectedExtents.Cast<object>().SequenceEqual(result.Contents.Cast<object>())); // Teardown }
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 }
public void AddToBasketWillUseRepositoryCorrectly() { // Fixture setup var fixture = new RepositoryFixture(); var productId = fixture.CreateAnonymous <int>(); var quantity = fixture.CreateAnonymous <int>(); var user = fixture.CreateAnonymous <IPrincipal>(); var repositoryMock = new Mock <BasketRepository>(); repositoryMock.Setup(r => r.AddToBasket(productId, quantity, user)).Verifiable(); fixture.Register(() => repositoryMock.Object); var sut = fixture.CreateAnonymous <BasketService>(); // Exercise system sut.AddToBasket(productId, quantity, user); // Verify outcome repositoryMock.Verify(); // 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, 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 }
public void ApplyWillApplyDiscountFromRepository() { // Fixture setup var fixture = new RepositoryFixture(); var discountedProducts = fixture.CreateMany <Product>().ToList(); fixture.Register(() => { var repositoryStub = new Mock <DiscountRepository>(); repositoryStub.Setup(r => r.GetDiscountedProducts()).Returns(discountedProducts); return(repositoryStub.Object); }); var basket = fixture.CreateAnonymous <Basket>(); basket.Contents.Add(new Extent(discountedProducts.First().WithUnitPrice(discountedProducts.First().UnitPrice.Add(fixture.CreateAnonymous <decimal>()))) { Quantity = fixture.CreateAnonymous <int>() }); var expectedExtent = new { Product = new Likeness <Product, Product>(discountedProducts.First()).Without(d => d.Id), basket.Contents.First().Quantity }.AsSource().OfLikeness <Extent>() .Without(d => d.Total) .Without(d => d.Updated); var sut = fixture.CreateAnonymous <RepositoryBasketDiscountPolicy>(); // Exercise system var result = sut.Apply(basket); // Verify outcome Assert.True(expectedExtent.Equals(result.Contents.First())); // 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, 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 }
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 }
public void EmptyWillUseRepositoryCorrectly() { // Fixture setup var fixture = new RepositoryFixture(); var user = fixture.CreateAnonymous<IPrincipal>(); var repositoryMock = new Mock<BasketRepository>(); repositoryMock.Setup(r => r.Empty(user)).Verifiable(); fixture.Register(() => repositoryMock.Object); var sut = fixture.CreateAnonymous<BasketService>(); // Exercise system sut.Empty(user); // Verify outcome repositoryMock.Verify(); // Teardown }
public void AddToBasketWillUseRepositoryCorrectly() { // Fixture setup var fixture = new RepositoryFixture(); var productId = fixture.CreateAnonymous<int>(); var quantity = fixture.CreateAnonymous<int>(); var user = fixture.CreateAnonymous<IPrincipal>(); var repositoryMock = new Mock<BasketRepository>(); repositoryMock.Setup(r => r.AddToBasket(productId, quantity, user)).Verifiable(); fixture.Register(() => repositoryMock.Object); var sut = fixture.CreateAnonymous<BasketService>(); // Exercise system sut.AddToBasket(productId, quantity, user); // Verify outcome repositoryMock.Verify(); // Teardown }
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 }
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 }
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, Product>(p) .With(d => d.UnitPrice).EqualsWhen((s, d) => s.UnitPrice.Multiply(.95m).Equals(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 }