简体   繁体   English

Mediatr 模式单元测试

[英]Mediatr pattern unit testing

I'm trying to setup unit testing for my API controllers.我正在尝试为我的 API 控制器设置单元测试。 I'm using the mediatr pattern and FakeIteasy.我正在使用 mediatr 模式和 FakeIteasy。 I have the following code.我有以下代码。

public class ChannelGroupChannelsControllerTests
{
    private readonly ChannelGroupChannelsController _controller;
    private readonly IMediator _mediator;

    public ChannelGroupChannelsControllerTests()
    {
        var service = A.Fake<IReadChannelGroupChannel>();
        var mapper = A.Fake<IMapper>();

        var channelGroupChannel = new ChannelGroupChannel
        {
            Id = 1,
            ChannelGroupId = 1,
            ChannelId = 1,
            Channel = new Channel { Name = "Channel Name" }
        };

        _mediator = A.Fake<IMediator>();
        _controller = new ChannelGroupChannelsController(_mediator, mapper);

        A.CallTo(() => _mediator.Send(A<GetChannelGroupChannelById>._, A<CancellationToken>._)).Returns(channelGroupChannel);
    }


    [Fact]
    public async Task ChannelGroupChannelsController_ById()
    {
        var result = await _controller.ById(1);

        (result.Result as StatusCodeResult)?.StatusCode.Should().Be((int)HttpStatusCode.OK);
        result.Value.Should().BeOfType<ChannelGroupChannelVM>();
    }
}

Now the problem is that I keep getting NULL as a value.现在的问题是我不断将 NULL 作为一个值。 I think the issue might be that GetChannelGroupChannelById has a constructor that expects the ID.我认为问题可能是 GetChannelGroupChannelById 有一个需要 ID 的构造函数。 But I'm not sure...但我不确定...

Does anybody know what could be wrong?有谁知道可能出了什么问题? I'm pretty new with the mocking stuff.我对 mocking 的东西很陌生。

Kind regards亲切的问候

I'm not familiar with mediatr, so may be off base here, and it would be much easier to answer this question if we saw what your controller was doing.我对 mediatr 不熟悉,所以这里可能不合适,如果我们看到您的 controller 在做什么,回答这个问题会容易得多。 If you're able, please supply the code, as without that insight, I'm left to guess a little, but I'll try.如果可以,请提供代码,因为没有这种见解,我只能猜测一下,但我会尝试。

If GetChannelGroupChannelById 's constructor expects an ID (an int ?), FakeItEasy will provide an ID when it makes the Fake version.如果GetChannelGroupChannelById的构造函数需要一个 ID(一个int ?),FakeItEasy 将在制作 Fake 版本时提供一个 ID。 If it's an int , FakeItEasy will provide a 0 , unless you've done some very fancy configuration you've not shown us.如果它是一个int ,FakeItEasy 将提供一个0 ,除非你做了一些你没有向我们展示的非常奇特的配置。 If that's supposed to line up with some other value in your code and doesn't, it may cause your problem.如果这应该与您的代码中的其他一些值一致并且没有,则可能会导致您的问题。

Otherwise, I see you have a Fake IMapper , that is never configured, but is passed into the controller.否则,我看到你有一个假的IMapper ,它从未配置过,但被传递到 controller 中。 I'm guessing this is supposed to translate some values.我猜这应该翻译一些值。 An unconfigured Fake will return a dummy value (or default if no dummy value can be made). 未配置的 Fake将返回一个虚拟值(如果无法生成虚拟值,则返回default值)。 It's possible that this unconfigured mapper is interrupting your flow.这个未配置的映射器可能会中断您的流程。

(I also see that service in the test class constructor is unused. I would remove it or use it. It may not be part of your problem, but it's at least distracting.) (我还看到测试 class 构造函数中的service未使用。我会删除它或使用它。它可能不是你问题的一部分,但它至少会分散注意力。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM