Skip to content

Commit 6eb8e8c

Browse files
committed
test: 更新单元测试
1 parent b3e6d57 commit 6eb8e8c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ public static class ICacheEntryExtensions
1717
/// 获得缓存项 <see cref="ICacheEntry"/> 最后访问时间
1818
/// </summary>
1919
/// <param name="entry"></param>
20+
/// <param name="force"></param>
2021
/// <returns></returns>
21-
public static DateTime? GetLastAccessed(this ICacheEntry entry)
22+
public static DateTime? GetLastAccessed(this ICacheEntry entry, bool force = false)
2223
{
24+
if (force)
25+
{
26+
_lastAccessedProperty = null;
27+
}
2328
_lastAccessedProperty ??= entry.GetType().GetProperty("LastAccessed", BindingFlags.Instance | BindingFlags.NonPublic);
2429

2530
DateTime? ret = null;

test/UnitTest/Extensions/ICacheEntryExtensionsTest.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6+
using Microsoft.Extensions.Caching.Memory;
7+
using Microsoft.Extensions.Primitives;
8+
69
namespace UnitTest.Extensions;
710

811
public class ICacheEntryExtensionsTest : BootstrapBlazorTestBase
@@ -16,7 +19,42 @@ public void GetLastAccessed_Ok()
1619
});
1720

1821
Assert.True(Cache.TryGetCacheEntry("test_01", out var entry));
19-
var v = entry.GetLastAccessed();
22+
var v = entry.GetLastAccessed(true);
2023
Assert.NotNull(v);
2124
}
25+
26+
[Fact]
27+
public void GetLastAccessed_Null()
28+
{
29+
var mock = new MockCacheEntry();
30+
var v = mock.GetLastAccessed(true);
31+
Assert.Null(v);
32+
}
33+
34+
class MockCacheEntry : ICacheEntry
35+
{
36+
public object Key { get; }
37+
public object? Value { get; set; }
38+
public DateTimeOffset? AbsoluteExpiration { get; set; }
39+
public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; }
40+
public TimeSpan? SlidingExpiration { get; set; }
41+
public IList<IChangeToken> ExpirationTokens { get; }
42+
public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; }
43+
public CacheItemPriority Priority { get; set; }
44+
public long? Size { get; set; }
45+
46+
private int LastAccessed { get; set; }
47+
48+
public MockCacheEntry()
49+
{
50+
Key = "_test";
51+
ExpirationTokens = [];
52+
PostEvictionCallbacks = [];
53+
}
54+
55+
public void Dispose()
56+
{
57+
throw new NotImplementedException();
58+
}
59+
}
2260
}

0 commit comments

Comments
 (0)