コード例 #1
0
        public async Task Get_WithBadId_Within_CacheEntryExpirationDate_ShouldOnlyCallService_Once(string badId)
        {
            var cache = new MemoryCache(new MemoryCacheOptions());

            var mockedService = new Mock <IOrgDataService>();

            mockedService
            .Setup(x => x.GetOrgDataById(badId))
            .Returns(Task.FromResult <OrgData?>(null));

            var controller = new OrgDataController(mockedService.Object, cache);

            await controller.Get(badId);

            await controller.Get(badId);

            cache.Count.Should().Be(1);
            mockedService.Verify(x => x.GetOrgDataById(badId), Times.Once);
        }
コード例 #2
0
        public async Task Get_WithInterval_Over_CacheEntryExpirationDate_ShouldCallServiceTwice()
        {
            var          cache   = new MemoryCache(new MemoryCacheOptions());
            const string validId = "123123123";

            var testData = new OrgData(validId, "testname", "test");

            var mockedService = new Mock <IOrgDataService>();

            mockedService
            .Setup(x => x.GetOrgDataById(validId))
            .Returns(Task.FromResult <OrgData?>(testData));

            var controller = new OrgDataController(mockedService.Object, cache);

            await controller.Get(validId);

            await Task.Delay(TimeSpan.FromSeconds(31));

            await controller.Get(validId);

            cache.Count.Should().Be(1);
            mockedService.Verify(x => x.GetOrgDataById(validId), Times.Exactly(2));
        }