简体   繁体   English

使用 NSubstitute 单元测试 Hangfire

[英]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.

相关问题 使用NSubstitute的单元测试无效方法 - Unit test void method with NSubstitute 使用NUnit和NSubstitute对通用存储库进行单元测试 - Unit test of generic repository using NUnit and NSubstitute 异步的单元测试与FluentAssertions和NSubstitute无关 - Unit Test with Async is Inconclusive with FluentAssertions and NSubstitute 验证表达式<Func<T, bool> &gt; 使用 NSubstitute 进行单元测试的参数 - Verify Expression<Func<T, bool>> argument in a unit test with NSubstitute 如何使用 NSubstitute 在单元测试中模拟 MassTransit Header 值 - How to mock MassTransit Header values in unit test using NSubstitute 使用 MongoDB 进行单元测试(NUnit、Nsubstitute)ASP 核心服务 - Unit test (NUnit, Nsubstitute) ASP Core Service with MongoDB 使用 NSubstitute 进行 MVC 单元测试中的 Mocking 用户 IP 地址 - Mocking user IP address in MVC unit test with NSubstitute C#单元测试NSubstitute无法在ObjectCache中设置值 - C# Unit Test NSubstitute Unable to Set Value in ObjectCache 如何对使用 DbContext 和 NSubstitute 的存储库进行单元测试? - How do I unit test a repository that uses DbContext with NSubstitute? MVC4单元测试NSubstitute无法找到从中返回的调用 - MVC4 Unit test NSubstitute Could not find a call to return from
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM