コード例 #1
0
        public async Task ShouldGetTrafficForRegionFromCacheSuccessfully()
        {
            _traffic1.UpdatedAt = DateTime.Now;

            var trafficCache = new Mock <ITrafficCache>();

            trafficCache.Setup(p => p.GetByRegionCode(_region1.Code)).Returns(_traffic1);

            var trafficProvider = new Mock <ITrafficProvider>();

            trafficProvider.Setup(p => p.GetTrafficAsync(_region1.Code)).Returns(Task.Run(() => _traffic1));

            var regionService = new Mock <IRegionService>();

            var service = new CachedTrafficService(trafficCache.Object, trafficProvider.Object, regionService.Object);
            var result  = await service.GetTrafficForRegionAsync(_region1);

            Assert.IsNotNull(result);
            Assert.AreEqual(_traffic1.RegionCode, result.RegionCode);
            Assert.AreEqual(_traffic1.Level, result.Level);
            Assert.AreEqual(_traffic1.Hint, result.Hint);

            trafficCache.Verify(tc => tc.Save(It.IsAny <TrafficModel>()), Times.Never());
            trafficProvider.Verify(tc => tc.GetTrafficAsync(_region1.Code), Times.Never());
        }
コード例 #2
0
        public async Task ShouldGetTrafficForRegionWithEmptyCacheSuccessfully()
        {
            TrafficModel cachedValue = null;

            var trafficCache = new Mock <ITrafficCache>();

            trafficCache.Setup(p => p.GetByRegionCode(_region1.Code)).Returns(() =>
            {
                return(cachedValue);
            });
            trafficCache.Setup(p => p.Save(_traffic1)).Callback((TrafficModel traffic) =>
            {
                cachedValue = traffic;
            });

            var trafficProvider = new Mock <ITrafficProvider>();

            trafficProvider.Setup(p => p.GetTrafficAsync(_region1.Code)).Returns(Task.Run(() => _traffic1));

            var regionService = new Mock <IRegionService>();

            var service = new CachedTrafficService(trafficCache.Object, trafficProvider.Object, regionService.Object);
            var result  = await service.GetTrafficForRegionAsync(_region1);

            Assert.IsNotNull(result);
            Assert.AreEqual(_traffic1.RegionCode, result.RegionCode);
            Assert.AreEqual(_traffic1.Level, result.Level);
            Assert.AreEqual(_traffic1.Hint, result.Hint);

            trafficCache.Verify(tc => tc.Save(It.IsAny <TrafficModel>()), Times.Once());
            trafficProvider.Verify(tc => tc.GetTrafficAsync(_region1.Code), Times.Once());
        }