[英]How to use xunit to unit test contains the function of hangfire
開發語言是 dotnet core 3.0。 我不知道。幫幫我!非常感謝。
功能如下:
function test(){
BackgroundJob.Schedule<TradeCenterService>((s) => s.HandleSettlementJob(recordId), TimeSpan.FromSeconds(60));
}
我在測試類中配置了 hanfire,就像這樣
[DependsOn(typeof (AbpHangfireModule))]
public class MyProjectWebModule : AbpModule
{
public override void PreInitialize()
{
Configuration.BackgroundJobs.UseHangfire();
}
//...
}
現在測試結果錯誤信息是:
System.InvalidOperationException : JobStorage.Current property value has not been initialized. You must set it before using Hangfire Client or Server API.
堆棧跟蹤:
JobStorage.get_Current()
BackgroundJobClient.ctor()
<.cctor>b__45_0()
Lazy`1.PublicationOnlyViaFactory(LazyHelper initializer)
Lazy`1.CreateValue()
Lazy`1.get_Value()
在 Hangfire 的文檔頁面上,您有一個關於如何為依賴於 Hangfire 的方法編寫單元測試的示例。 重要的是要強調您仍然需要模擬它。 使用 Moq,它會是這樣的: new Mock<IBackgroundJobClient>();
[TestMethod]
public void CheckForSpamJob_ShouldBeEnqueued()
{
// Arrange
var client = new Mock<IBackgroundJobClient>();
var controller = new HomeController(client.Object);
var comment = CreateComment();
// Act
controller.Create(comment);
// Assert
client.Verify(x => x.Create(
It.Is<Job>(job => job.Method.Name == "CheckForSpam" && job.Args[0] == comment.Id),
It.IsAny<EnqueuedState>());
}
來源: https : //docs.hangfire.io/en/latest/background-methods/writing-unit-tests.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.