Skip to content

Commit

Permalink
test: 更新单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
ArgoZhang committed Jan 26, 2025
1 parent b3e6d57 commit 6eb8e8c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ public static class ICacheEntryExtensions
/// 获得缓存项 <see cref="ICacheEntry"/> 最后访问时间
/// </summary>
/// <param name="entry"></param>
/// <param name="force"></param>
/// <returns></returns>
public static DateTime? GetLastAccessed(this ICacheEntry entry)
public static DateTime? GetLastAccessed(this ICacheEntry entry, bool force = false)
{
if (force)
{
_lastAccessedProperty = null;
}
_lastAccessedProperty ??= entry.GetType().GetProperty("LastAccessed", BindingFlags.Instance | BindingFlags.NonPublic);

DateTime? ret = null;
Expand Down
40 changes: 39 additions & 1 deletion test/UnitTest/Extensions/ICacheEntryExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Primitives;

namespace UnitTest.Extensions;

public class ICacheEntryExtensionsTest : BootstrapBlazorTestBase
Expand All @@ -16,7 +19,42 @@ public void GetLastAccessed_Ok()
});

Assert.True(Cache.TryGetCacheEntry("test_01", out var entry));
var v = entry.GetLastAccessed();
var v = entry.GetLastAccessed(true);
Assert.NotNull(v);
}

[Fact]
public void GetLastAccessed_Null()
{
var mock = new MockCacheEntry();
var v = mock.GetLastAccessed(true);
Assert.Null(v);
}

class MockCacheEntry : ICacheEntry
{
public object Key { get; }
public object? Value { get; set; }
public DateTimeOffset? AbsoluteExpiration { get; set; }
public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; }
public TimeSpan? SlidingExpiration { get; set; }
public IList<IChangeToken> ExpirationTokens { get; }
public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; }
public CacheItemPriority Priority { get; set; }
public long? Size { get; set; }

private int LastAccessed { get; set; }

public MockCacheEntry()
{
Key = "_test";
ExpirationTokens = [];
PostEvictionCallbacks = [];
}

public void Dispose()
{
throw new NotImplementedException();
}
}
}

0 comments on commit 6eb8e8c

Please sign in to comment.