我正在尝试对我的类进行单元测试,其中包括一个后台工作。到目前为止,我正在测试的方法将一个作业排入队列,如下所示:
public void SendSms(SmsContent content){
....
_backgroundJobClient.Enqueue<ISms>(x => x.Send(content));
....
}我的第一个单元测试检查BackgroundJobClient是否被调用,如下所示:
Assert.Equal(1,_backgroundJobClient.ReceivedCalls().Count());所有的工作都很好,但现在我想检查所有的参数,如果正确发送。我正在查看HangFire documentation,但我不知道如何使用NSubstitute进行测试。
谢谢!
发布于 2021-10-28 16:43:28
使用以下代码:
_backgroundJobClient.ReceivedWithAnyArgs()
.Enqueue<ISms>(x => x.Send(default));发布于 2021-10-16 00:46:05
基于example in the docs,您应该能够使用以下内容对其进行测试:
/* 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>()
);这只是基于文档中的示例。您需要的确切arg匹配代码将取决于您所使用的特定类型。
https://stackoverflow.com/questions/69587243
复制相似问题