예제 #1
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
        }
        public void ContentsWillBeCorrect()
        {
            // Fixture setup
            var fixture = new ControllerFixture();
            
            var basket = fixture.CreateAnonymous<Basket>();
            fixture.AddManyTo(basket.Contents);

            var expectedResult = (from e in basket.Contents
                                  select string.Format("{0} {1}: {2:C}", e.Quantity, e.Product.Name, e.Total)).ToList();

            var sut = fixture.Build<BasketViewModel>()
                .FromFactory(() => new BasketViewModel(basket))
                .OmitAutoProperties()
                .CreateAnonymous();
            // Exercise system
            IEnumerable<string> result = sut.Contents;
            // Verify outcome
            Assert.True(expectedResult.SequenceEqual(result));
            // Teardown
        }
예제 #3
0
        public void ContentsWillBeCorrect()
        {
            // Fixture setup
            var fixture = new ControllerFixture();

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

            fixture.AddManyTo(basket.Contents);

            var expectedResult = (from e in basket.Contents
                                  select string.Format("{0} {1}: {2:C}", e.Quantity, e.Product.Name, e.Total)).ToList();

            var sut = fixture.Build <BasketViewModel>()
                      .FromFactory(() => new BasketViewModel(basket))
                      .OmitAutoProperties()
                      .CreateAnonymous();
            // Exercise system
            IEnumerable <string> result = sut.Contents;

            // Verify outcome
            Assert.True(expectedResult.SequenceEqual(result));
            // Teardown
        }
예제 #4
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
        }
        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 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
 }