public void CacheTimeoutWillMatchConstructorArgument() { // Fixture setup var fixture = new Fixture(); var expectedTimeout = fixture.CreateAnonymous <TimeSpan>(); var sut = new CachingCurrency(new Mock <Currency>().Object, expectedTimeout); // Exercise system TimeSpan result = sut.CacheTimeout; // Verify outcome Assert.Equal <TimeSpan>(expectedTimeout, result); // Teardown (implicit) }
public override Currency GetCurrency(string currencyCode) { CachingCurrency currency; if (this.cache.TryGetValue(currencyCode, out currency)) { return(currency); } currency = new CachingCurrency(this.innerProvider.GetCurrency(currencyCode), this.CacheTimeout); this.cache.Add(currencyCode, currency); return(currency); }
public void InnerCurrencyIsInvokedAgainWhenCacheExpires() { // Fixture setup var currencyCode = "CHF"; var cacheTimeout = TimeSpan.FromHours(1); var startTime = new DateTime(2009, 8, 29); var timeProviderStub = new Mock <TimeProvider>(); timeProviderStub .SetupGet(tp => tp.UtcNow) .Returns(startTime); TimeProvider.Current = timeProviderStub.Object; var innerCurrencyMock = new Mock <Currency>(); innerCurrencyMock .Setup(c => c.GetExchangeRateFor(currencyCode)) .Returns(4.911m) .Verifiable(); var sut = new CachingCurrency(innerCurrencyMock.Object, cacheTimeout); sut.GetExchangeRateFor(currencyCode); sut.GetExchangeRateFor(currencyCode); sut.GetExchangeRateFor(currencyCode); timeProviderStub .SetupGet(tp => tp.UtcNow) .Returns(startTime + cacheTimeout); // Exercise system sut.GetExchangeRateFor(currencyCode); // Verify outcome innerCurrencyMock.Verify( c => c.GetExchangeRateFor(currencyCode), Times.Exactly(2)); // Teardown (implicit) }
public void GetExchangeRateOnceWillReturnRateFromInnerCurrency(decimal expectedExchangeRate, string currencyCode, [Frozen] Mock <Currency> innerCurrencyStub, CachingCurrency sut) { // Fixture setup innerCurrencyStub.Setup(c => c.GetExchangeRateFor(currencyCode)).Returns(expectedExchangeRate); // Exercise system var result = sut.GetExchangeRateFor(currencyCode); // Verify outcome Assert.Equal <decimal>(expectedExchangeRate, result); // Teardown (implicit) }