来自 API 控制器的单元测试 NService.Send

Unit Test NService.Send from an API Controller

我有一个 API 控制器,它使用 NServiceBus 发布命令。我正在使用 NUnit 和 NSubstitute 进行测试。我想测试模型中的某些属性是否已填充到命令

这是我的带有路线的控制器。

[RoutePrefix("api/fileService")]
public class FileServiceController : ApiController
{
    [HttpPost]
    [Route("releasefile")]
    public async Task<IHttpActionResult> ReleaseFile(FileReleaseAPIModels.ReleaseFileModel model)
    {
        var currentUser = RequestContext.Principal?.Identity as ClaimsIdentity;


        if (model.FileType.Equals("ProductFile"))
        {
            _logger.Info($"Releasing Product files for date: {model.FileDate.ToShortDateString()} ");
            _bus.Send<IReleaseProductFiles>("FileManager.Service", t =>
            {
                t.FileId = Guid.NewGuid();
                t.RequestedDataDate = model.FileDate;
                t.RequestingUser = currentUser?.Name;
                t.RequestDateTime = DateTime.Now;
            });
        }
        return Ok();
    }

}

在我的测试中,我替换(模拟)Ibus 并尝试验证收到的呼叫。下面是测试方法:

    [Test]
    public async Task TestReleaseProductsFile()
    {
        var bus = Substitute.For<IBus>();
        var dbContent = _container.Resolve<IFileManagerDbContext>();
        var apiContext = new FileServiceController(bus, dbContent);

        //Create a snapshot
        var releaseDate = DateTime.Now.Date;
        var result = await apiContext.ReleaseFile(new ReleaseFileModel
        {
            FileDate = releaseDate,
            FileType = "ProductFile"
        });

        Assert.That(result, Is.Not.Null, "Result is null");

        Assert.That(result, Is.TypeOf<OkResult>(), "Status code is not ok");

        bus.Received(1)
            .Send<IReleaseProductFiles>(Arg.Is<string>("FileManager.Service"), Arg.Is<Action<IReleaseProductFiles>>(
                action =>
                {
                    action.FileId = Guid.NewGuid();
                    action.RequestedDataDate = releaseDate;
                    action.RequestingUser = String.Empty;
                    action.RequestDateTime = DateTime.Now;
                }));
    }

这会导致错误 - 即使消息已实际发送。这是错误消息:

NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 1 call matching:
    Send<IReleaseProductFiles>("Capelogic.Service", Action<IReleaseProductFiles>)
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
    Send<IReleaseProductFiles>("Capelogic.Service", *Action<IReleaseProductFiles>*)

我显然遗漏了一些明显的东西。

这里的问题在于 SendAction<IReleaseProductFiles> 参数——我们无法自动判断两个不同的操作是否相同。相反,NSubstitute 依赖于等效的引用。因为测试和生产代码都创建了自己的 Action 实例,所以它们总是不同的,NSubstitute 会说调用不匹配。

有一个few different options for testing this。这些示例与 Expression<Func<>> 相关,但同样的想法适用于 Action<>s.

在这种情况下,我很想间接测试一下:

[Test]
public async Task TestReleaseProductsFile()
{
    var bus = Substitute.For<IBus>();
    var returnedProductFiles = Substitute.For<IReleaseProductFiles>();

    // Whenever bus.Send is called with "FileManager.Service" arg, invoke
    // the given callback with the `returnedProductFiles` object.
    // We can then make sure the action updates that object as expected.
    bus.Send<IReleaseProductFiles>(
            "FileManager.Service",
            Arg.Invoke<IReleaseProductFiles>(returnedProductFiles));

    // ... remainder of test ...

    Assert.That(result, Is.TypeOf<OkResult>(), "Status code is not ok");
    Assert.That(returnedProductFiles.FileId, Is.Not.EqualTo(Guid.Empty));
    Assert.That(returnedProductFiles.RequestedDataDate, Is.EqualTo(releaseDate));
    Assert.That(returnedProductFiles.RequestingUser, Is.EqualTo(String.Empty));
}

我建议您仔细阅读前面提到的答案,看看是否有更适合您情况的答案。