public ViewResult Index() { var currencyCode = this.CurrencyProfileService.GetCurrencyCode(); var currency = this.currencyProvider.GetCurrency(currencyCode); var basket = this.basketService .GetBasketFor(this.User) .ConvertTo(currency); if (basket.Contents.Count == 0) { return this.View("Empty"); } var vm = new BasketViewModel(basket); return this.View(vm); }
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 }