使用 TimeProvider 类注入时间

This commit is contained in:
administrator committed 2025-03-15 21:15:59 +08:00
1 parent 81c8f608dc
commit c10d9726a2
13 files changed
+28 -61

No files matched your search

@@ -13,6 +13,7 @@
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="9.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.3" />
@@ -9,6 +9,7 @@ using HelloShop.ProductService.Infrastructure;
using HelloShop.ProductService.Models.Products;
using HelloShop.ProductService.UnitTests.Helpers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Time.Testing;
namespace HelloShop.ProductService.UnitTests
{
@@ -60,5 +61,16 @@ namespace HelloShop.ProductService.UnitTests
Assert.Equal(productName, result?.Name);
});
}
[Fact]
public async Task UseTimeProviderTestService()
{
FakeTimeProvider fakeTimeProvider = new();
fakeTimeProvider.SetUtcNow(fakeTimeProvider.GetUtcNow().AddDays(1));
Assert.Equal(DateTime.UtcNow.AddHours(1), fakeTimeProvider.GetUtcNow());
await Task.CompletedTask;
}
}
}