Skip to content

Commit 591ec4e

Browse files
committed
One more test
1 parent 07cdc67 commit 591ec4e

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Tests/Drogecode.Blazor.ExpireStorage.Tests/Mocks/LocalStorageServiceMock.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ public LocalStorageServiceMock(IMemoryCache memoryCache)
1818

1919
public async ValueTask<T?> GetItemAsync<T>(string key, CancellationToken cancellationToken = new CancellationToken())
2020
{
21-
throw new NotImplementedException();
21+
if (_memoryCache.TryGetValue(key, out var value))
22+
{
23+
return (T)value!;
24+
}
25+
return default;
2226
}
2327

2428
public async ValueTask<string?> GetItemAsStringAsync(string key, CancellationToken cancellationToken = new CancellationToken())

Tests/Drogecode.Blazor.ExpireStorage.Tests/Tests/Services/ExpireStorageServiceTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,26 @@ public async Task ByFunctionTest()
3232
response.HandledBy.Should().Be(HandledBy.Function);
3333
response.Data.Should().Be("test");
3434
}
35+
36+
[Fact]
37+
public async Task FromCacheTest()
38+
{
39+
const string cacheKey = "FromCacheTest";
40+
var addToCache = await _expireStorageService.CachedRequestAsync<TestStringResponse>(cacheKey, () => Task.FromResult(new TestStringResponse
41+
{
42+
Data = "test"
43+
}),
44+
clt: TestContext.Current.CancellationToken);
45+
Assert.NotNull(addToCache?.Data);
46+
addToCache.HandledBy.Should().Be(HandledBy.Function);
47+
var response = await _expireStorageService.CachedRequestAsync<TestStringResponse>(cacheKey, () => Task.FromResult(new TestStringResponse
48+
{
49+
Data = "not called"
50+
}),
51+
new CachedRequest { OneCallPerCache = true },
52+
clt: TestContext.Current.CancellationToken);
53+
Assert.NotNull(response?.Data);
54+
response.HandledBy.Should().Be(HandledBy.Cache);
55+
response.Data.Should().Be("test");
56+
}
3557
}

0 commit comments

Comments
 (0)