Exemple #1
0
        public void IndexWillUseCurrencyFromCurrencyProfileService()
        {
            // Fixture setup
            var fixture      = new ControllerFixture();
            var currencyCode = fixture.CreateAnonymous("CurrencyCode");

            var currencyProfileServiceStub = new Mock <CurrencyProfileService>();

            currencyProfileServiceStub.Setup(cps => cps.GetCurrencyCode()).Returns(currencyCode);

            var currencyProviderMock = new Mock <CurrencyProvider>();

            currencyProviderMock.Setup(cp => cp.GetCurrency(currencyCode)).Returns(fixture.CreateAnonymous <Currency>()).Verifiable();
            fixture.Register(() => currencyProviderMock.Object);

            fixture.Register(() =>
            {
                var basketServiceStub = new Mock <IBasketService>();
                basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny <IPrincipal>())).Returns(fixture.CreateAnonymous <Basket>());
                return(basketServiceStub.Object);
            });

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

            sut.CurrencyProfileService = currencyProfileServiceStub.Object;
            // Exercise system
            sut.Index();
            // Verify outcome
            currencyProviderMock.Verify();
            // Teardown
        }
        public void IndexWillReturnItemsWithCorrectCurrency()
        {
            // Fixture setup
            var fixture          = new ControllerFixture();
            var featuredProducts = fixture.CreateMany <Product>().ToList();

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

            var expectedCurrencyCode = fixture.CreateAnonymous("CurrencyCode");
            var currencyStub         = new Mock <Currency>();

            currencyStub.SetupGet(c => c.Code).Returns(expectedCurrencyCode);
            currencyStub.Setup(c => c.GetExchangeRateFor(It.IsAny <string>())).Returns(fixture.CreateAnonymous <decimal>());
            fixture.Register(() => currencyStub.Object);

            var sut = fixture.CreateAnonymous <HomeController>();
            // Exercise system
            var result = ((FeaturedProductsViewModel)sut.Index().ViewData.Model).Products.Select(pvm => pvm.UnitPrice.CurrencyCode);

            // Verify outcome
            Assert.True(result.All(code => code == expectedCurrencyCode), "Index should convert to correct currency.");
            // Teardown
        }
Exemple #3
0
        public void IndexWillReturnCorrectContentsWhenProfileRequestsConversion()
        {
            // Fixture setup
            var fixture = new ControllerFixture();
            var basket  = fixture.CreateAnonymous <Basket>();

            fixture.AddManyTo(basket.Contents);

            var currencyCode          = fixture.CreateAnonymous("CurrencyCode");
            var anonymousExchangeRate = 3.2m;

            var currencyStub = new Mock <Currency>();

            currencyStub.SetupGet(c => c.Code).Returns(currencyCode);
            currencyStub.Setup(c => c.GetExchangeRateFor(It.IsAny <string>())).Returns(anonymousExchangeRate);

            fixture.Register(() =>
            {
                var basketServiceStub = new Mock <IBasketService>();
                basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny <IPrincipal>())).Returns(basket);
                return(basketServiceStub.Object);
            });
            fixture.Customize <UserProfile>(ob => ob.FromFactory(() =>
            {
                var profileStub = new Mock <UserProfile>();
                profileStub.SetupGet(p => p.Currency).Returns(currencyCode);
                return(profileStub.Object);
            }).OmitAutoProperties());
            fixture.Register(() =>
            {
                var currencyProviderStub = new Mock <CurrencyProvider>();
                currencyProviderStub.Setup(cp => cp.GetCurrency(currencyCode)).Returns(currencyStub.Object);
                return(currencyProviderStub.Object);
            });

            var expectedContents = new BasketViewModel(basket.ConvertTo(currencyStub.Object)).Contents.ToList();

            var sut = fixture.CreateAnonymous <BasketController>();
            // Exercise system
            var result = ((BasketViewModel)sut.Index().ViewData.Model).Contents.ToList();

            // Verify outcome
            Assert.True(expectedContents.SequenceEqual(result));
            // Teardown
        }
Exemple #4
0
        public void EmptyWillEmptyBasketFromService()
        {
            // Fixture setup
            var fixture = new ControllerFixture();

            var user = new Mock <IPrincipal>().Object;

            fixture.Register(() => user);

            var repositoryStub = new Mock <IBasketService>();

            repositoryStub.Setup(r => r.Empty(user)).Verifiable();

            fixture.Register(() => repositoryStub.Object);

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

            // Exercise system
            sut.Empty();
            // Verify outcome
            repositoryStub.Verify();
            // Teardown
        }
Exemple #5
0
        public void IndexWillReturnCorrectViewWhenBasketIsEmpty()
        {
            // Fixture setup
            var fixture = new ControllerFixture();

            fixture.Register(() =>
            {
                var basketServiceStub = new Mock <IBasketService>();
                basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny <IPrincipal>())).Returns(fixture.CreateAnonymous <Basket>());
                return(basketServiceStub.Object);
            });
            var sut = fixture.CreateAnonymous <BasketController>();
            // Exercise system
            var result = sut.Index().ViewName;

            // Verify outcome
            Assert.Equal <string>("Empty", result);
            // Teardown
        }
Exemple #6
0
        public void IndexWillReturnInstance()
        {
            // Fixture setup
            var fixture = new ControllerFixture();

            fixture.Register(() =>
            {
                var basketServiceStub = new Mock <IBasketService>();
                basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny <IPrincipal>())).Returns(fixture.CreateAnonymous <Basket>());
                return(basketServiceStub.Object);
            });

            var sut = fixture.CreateAnonymous <BasketController>();
            // Exercise system
            ViewResult result = sut.Index();

            // Verify outcome
            Assert.NotNull(result);
            // Teardown
        }
Exemple #7
0
        public void IndexWillReturnViewWithCorrectModelWhenBasketHasContents()
        {
            // Fixture setup
            var fixture = new ControllerFixture();

            fixture.Register(() =>
            {
                var basketServiceStub = new Mock <IBasketService>();
                basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny <IPrincipal>())).Returns(fixture.Build <Basket>().Do(b => fixture.AddManyTo(b.Contents)).CreateAnonymous());
                return(basketServiceStub.Object);
            });
            var  sut = fixture.CreateAnonymous <BasketController>();
            Type expectedModelType = typeof(BasketViewModel);
            // Exercise system
            var result = sut.Index().ViewData.Model;

            // Verify outcome
            Assert.IsAssignableFrom <BasketViewModel>(result);
            // Teardown
        }
Exemple #8
0
        public void AddWillAddCorrectItemToBasketService()
        {
            // Fixture setup
            var fixture = new ControllerFixture();

            var productId = fixture.CreateAnonymous <int>();

            var repositoryStub = new Mock <IBasketService>();

            repositoryStub.Setup(r => r.AddToBasket(productId, 1, It.IsAny <IPrincipal>())).Verifiable();

            fixture.Register(() => repositoryStub.Object);

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

            // Exercise system
            sut.Add(productId);
            // Verify outcome
            repositoryStub.Verify();
            // Teardown
        }
        public void IndexWillUseCurrencyFromProfile()
        {
            // Fixture setup
            var fixture      = new ControllerFixture();
            var currencyCode = fixture.CreateAnonymous("CurrencyCode");

            var currencyProfileServiceStub = new Mock <CurrencyProfileService>();

            currencyProfileServiceStub.Setup(cps => cps.GetCurrencyCode()).Returns(currencyCode);

            var currencyProviderMock = new Mock <CurrencyProvider>();

            currencyProviderMock.Setup(cp => cp.GetCurrency(currencyCode)).Returns(fixture.CreateAnonymous <Currency>()).Verifiable();
            fixture.Register(() => currencyProviderMock.Object);

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

            sut.CurrencyProfileService = currencyProfileServiceStub.Object;
            // Exercise system
            sut.Index();
            // Verify outcome
            currencyProviderMock.Verify();
            // Teardown
        }
 public void IndexWillReturnViewWithCorrectModelWhenBasketHasContents()
 {
     // Fixture setup
     var fixture = new ControllerFixture();
     fixture.Register(() =>
     {
         var basketServiceStub = new Mock<IBasketService>();
         basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny<IPrincipal>())).Returns(fixture.Build<Basket>().Do(b => fixture.AddManyTo(b.Contents)).CreateAnonymous());
         return basketServiceStub.Object;
     });
     var sut = fixture.CreateAnonymous<BasketController>();
     Type expectedModelType = typeof(BasketViewModel);
     // Exercise system
     var result = sut.Index().ViewData.Model;
     // Verify outcome
     Assert.IsAssignableFrom<BasketViewModel>(result);
     // Teardown
 }
        public void IndexWillReturnInstance()
        {
            // Fixture setup
            var fixture = new ControllerFixture();

            fixture.Register(() =>
                {
                    var basketServiceStub = new Mock<IBasketService>();
                    basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny<IPrincipal>())).Returns(fixture.CreateAnonymous<Basket>());
                    return basketServiceStub.Object;
                });

            var sut = fixture.CreateAnonymous<BasketController>();
            // Exercise system
            ViewResult result = sut.Index();
            // Verify outcome
            Assert.NotNull(result);
            // Teardown
        }
        public void IndexWillReturnCorrectContentsWhenProfileRequestsConversion()
        {
            // Fixture setup
            var fixture = new ControllerFixture();
            var basket = fixture.CreateAnonymous<Basket>();
            fixture.AddManyTo(basket.Contents);

            var currencyCode = fixture.CreateAnonymous("CurrencyCode");
            var anonymousExchangeRate = 3.2m;

            var currencyStub = new Mock<Currency>();
            currencyStub.SetupGet(c => c.Code).Returns(currencyCode);
            currencyStub.Setup(c => c.GetExchangeRateFor(It.IsAny<string>())).Returns(anonymousExchangeRate);

            fixture.Register(() =>
                {
                    var basketServiceStub = new Mock<IBasketService>();
                    basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny<IPrincipal>())).Returns(basket);
                    return basketServiceStub.Object;
                });
            fixture.Customize<UserProfile>(ob => ob.FromFactory(() =>
                {
                    var profileStub = new Mock<UserProfile>();
                    profileStub.SetupGet(p => p.Currency).Returns(currencyCode);
                    return profileStub.Object;
                }).OmitAutoProperties());
            fixture.Register(() =>
                {
                    var currencyProviderStub = new Mock<CurrencyProvider>();
                    currencyProviderStub.Setup(cp => cp.GetCurrency(currencyCode)).Returns(currencyStub.Object);
                    return currencyProviderStub.Object;
                });

            var expectedContents = new BasketViewModel(basket.ConvertTo(currencyStub.Object)).Contents.ToList();

            var sut = fixture.CreateAnonymous<BasketController>();
            // Exercise system
            var result = ((BasketViewModel)sut.Index().ViewData.Model).Contents.ToList();
            // Verify outcome
            Assert.True(expectedContents.SequenceEqual(result));
            // Teardown
        }
        public void IndexWillUseCurrencyFromCurrencyProfileService()
        {
            // Fixture setup
            var fixture = new ControllerFixture();
            var currencyCode = fixture.CreateAnonymous("CurrencyCode");

            var currencyProfileServiceStub = new Mock<CurrencyProfileService>();
            currencyProfileServiceStub.Setup(cps => cps.GetCurrencyCode()).Returns(currencyCode);

            var currencyProviderMock = new Mock<CurrencyProvider>();
            currencyProviderMock.Setup(cp => cp.GetCurrency(currencyCode)).Returns(fixture.CreateAnonymous<Currency>()).Verifiable();
            fixture.Register(() => currencyProviderMock.Object);

            fixture.Register(() =>
                {
                    var basketServiceStub = new Mock<IBasketService>();
                    basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny<IPrincipal>())).Returns(fixture.CreateAnonymous<Basket>());
                    return basketServiceStub.Object;
                });

            var sut = fixture.CreateAnonymous<BasketController>();
            sut.CurrencyProfileService = currencyProfileServiceStub.Object;
            // Exercise system
            sut.Index();
            // Verify outcome
            currencyProviderMock.Verify();
            // Teardown
        }
        public void AddWillAddCorrectItemToBasketService()
        {
            // Fixture setup
            var fixture = new ControllerFixture();
            
            var productId = fixture.CreateAnonymous<int>();

            var repositoryStub = new Mock<IBasketService>();
            repositoryStub.Setup(r => r.AddToBasket(productId, 1, It.IsAny<IPrincipal>())).Verifiable();

            fixture.Register(() => repositoryStub.Object);

            var sut = fixture.CreateAnonymous<BasketController>();
            // Exercise system
            sut.Add(productId);
            // Verify outcome
            repositoryStub.Verify();
            // Teardown
        }
        public void EmptyWillEmptyBasketFromService()
        {
            // Fixture setup
            var fixture = new ControllerFixture();

            var user = new Mock<IPrincipal>().Object;
            fixture.Register(() => user);

            var repositoryStub = new Mock<IBasketService>();
            repositoryStub.Setup(r => r.Empty(user)).Verifiable();

            fixture.Register(() => repositoryStub.Object);

            var sut = fixture.CreateAnonymous<BasketController>();
            // Exercise system
            sut.Empty();
            // Verify outcome
            repositoryStub.Verify();
            // Teardown
        }
        public void IndexWillUseCurrencyFromProfile()
        {
            // Fixture setup
            var fixture = new ControllerFixture();
            var currencyCode = fixture.CreateAnonymous("CurrencyCode");

            var currencyProfileServiceStub = new Mock<CurrencyProfileService>();
            currencyProfileServiceStub.Setup(cps => cps.GetCurrencyCode()).Returns(currencyCode);

            var currencyProviderMock = new Mock<CurrencyProvider>();
            currencyProviderMock.Setup(cp => cp.GetCurrency(currencyCode)).Returns(fixture.CreateAnonymous<Currency>()).Verifiable();
            fixture.Register(() => currencyProviderMock.Object);

            var sut = fixture.CreateAnonymous<HomeController>();
            sut.CurrencyProfileService = currencyProfileServiceStub.Object;
            // Exercise system
            sut.Index();
            // Verify outcome
            currencyProviderMock.Verify();
            // Teardown
        }
        public void IndexWillReturnItemsWithCorrectCurrency()
        {
            // Fixture setup
            var fixture = new ControllerFixture();
            var featuredProducts = fixture.CreateMany<Product>().ToList();
            fixture.Register(() =>
            {
                var repositoryStub = new Mock<ProductRepository>();
                repositoryStub.Setup(r => r.GetFeaturedProducts()).Returns(featuredProducts);
                return repositoryStub.Object;
            });

            var expectedCurrencyCode = fixture.CreateAnonymous("CurrencyCode");
            var currencyStub = new Mock<Currency>();
            currencyStub.SetupGet(c => c.Code).Returns(expectedCurrencyCode);
            currencyStub.Setup(c => c.GetExchangeRateFor(It.IsAny<string>())).Returns(fixture.CreateAnonymous<decimal>());
            fixture.Register(() => currencyStub.Object);

            var sut = fixture.CreateAnonymous<HomeController>();
            // Exercise system
            var result = ((FeaturedProductsViewModel)sut.Index().ViewData.Model).Products.Select(pvm => pvm.UnitPrice.CurrencyCode);
            // Verify outcome
            Assert.True(result.All(code => code == expectedCurrencyCode), "Index should convert to correct currency.");
            // Teardown
        }
 public void IndexWillReturnCorrectViewWhenBasketIsEmpty()
 {
     // Fixture setup
     var fixture = new ControllerFixture();
     fixture.Register(() =>
         {
             var basketServiceStub = new Mock<IBasketService>();
             basketServiceStub.Setup(bs => bs.GetBasketFor(It.IsAny<IPrincipal>())).Returns(fixture.CreateAnonymous<Basket>());
             return basketServiceStub.Object;
         });
     var sut = fixture.CreateAnonymous<BasketController>();
     // Exercise system
     var result = sut.Index().ViewName;
     // Verify outcome
     Assert.Equal<string>("Empty", result);
     // Teardown
 }