[英]Unit test Hangfire with NSubstitute
I am trying to unit test my class which includes a background job.我正在尝试对我的课程进行单元测试,其中包括一项后台工作。 So far my method which I am testing Enqueues a job and looks like this:到目前为止,我正在测试的方法将作业排入队列,如下所示:
public void SendSms(SmsContent content){
....
_backgroundJobClient.Enqueue<ISms>(x => x.Send(content));
....
}
My first unit test check if BackgroundJobClient is called and looks like this:我的第一个单元测试检查是否调用了 BackgroundJobClient,如下所示:
Assert.Equal(1,_backgroundJobClient.ReceivedCalls().Count());
All works fine but now I want to check all the parameters if the are correctly sent.一切正常,但现在我想检查所有参数是否正确发送。 I was looking over the HangFire documentation but I couldn't figure that out how can be teste with NSubstitute.我正在查看HangFire 文档,但我无法弄清楚如何使用 NSubstitute 进行测试。
Thank you!谢谢!
Based on the example in the docs you should be able to test it using something like this:根据文档中的示例,您应该能够使用以下内容对其进行测试:
/* Moq example:
client.Verify(x => x.Create(
It.Is<Job>(job => job.Method.Name == "CheckForSpam" && job.Arguments[0] == comment.Id.ToString()),
It.IsAny<EnqueuedState>());
*/
// Ported to NSubstitute:
_backgroundJobClient.Received().Create(
Arg.Is<Job>(job => job.Method.Name == "CheckForSpam" && job.Arguments[0] == comment.Id.ToString()),
Arg.Any<EnqueuedState>()
);
This is only based on the documented example.这仅基于记录的示例。 The exact arg matching code you need will depend on the specific types you are using.您需要的确切 arg 匹配代码将取决于您使用的特定类型。
Use this code:使用此代码:
_backgroundJobClient.ReceivedWithAnyArgs()
.Enqueue<ISms>(x => x.Send(default));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.