简体   繁体   English

带有参数 Mediatr 和 Moq 的模拟处理程序

[英]Mock handler with parameter Mediatr and Moq

I'm trying to mock a handler with Moq.我正在尝试使用 Moq 模拟处理程序。 This handler takes a parameter of type bool to filter out active customers and non active customers.此处理程序采用 bool 类型的参数来过滤掉活跃客户和非活跃客户。

The handler is used in my service:处理程序用于我的服务:

    public async Task<IEnumerable<CustomerDto>> GetCustomers(bool active)
    {
        return _mapper.Map<IEnumerable<CustomerDto>>(await _mediatr.Send(new GetCustomersQuery { Active = active }));
    }

My handler looks like this:我的处理程序看起来像这样:

public class GetCustomersHandler : IRequestHandler<GetCustomersQuery, IEnumerable<Customer>>
{

    private readonly ICustomersRepository _repository;

    public GetCustomersHandler(ICustomersRepository repository)
    {
        _repository = repository;
    }

    public async Task<IEnumerable<Customer>> Handle(GetCustomersQuery request, CancellationToken cancellationToken)
    {
        return await _repository.GetCustomers(request.Active);
    }
}

My test:我的测试:

    [Fact]
    public async Task CustomersService_GetCustomers_ActiveReturnsOnlyActiveCustomers()
    {
        var result = await _service.GetCustomers(true);

        // asserts to test result
    }

My mock code:我的模拟代码:

        var mockMediatr = new Mock<IMediator>();
        mockMediatr.Setup(m => m.Send(It.IsAny<GetBlockedCustomersAndGroupsQuery>(), It.IsAny<CancellationToken>()))
            .Returns(async (bool active) => 
                await _getBlockedCustomersAndGroupsHandler.Handle(new GetBlockedCustomersAndGroupsQuery { Active = active }, new CancellationToken())); ---> How can I pass my bool parameter here?

EDIT: I don't have the mock code for the mediator in my test (for reuse).编辑:我的测试中没有中介的模拟代码(用于重用)。 I want to be able to test both scenarios where true is passed and false is passed.我希望能够测试传递 true 和传递 false 的两种情况。 If I try it like mentioned above, I get this error: "Invalid callback. Setup on method with 2 parameter(s) cannot invoke callback with different number of parameters (1)".如果我像上面提到的那样尝试,我会收到此错误:“无效的回调。具有 2 个参数的方法的设置无法使用不同数量的参数 (1) 调用回调”。

I can mock the mediator in the test code and pass that:我可以在测试代码中模拟中介并通过:

mockMediatr.Setup(m => m.Send(It.IsAny<GetBlockedCustomersAndGroupsQuery>(), It.IsAny<CancellationToken>()))
            .Returns(async () => 
                await _getBlockedCustomersAndGroupsHandler.Handle(new GetBlockedCustomersAndGroupsQuery { Active = true }, new CancellationToken()));

But here I'm not able to reuse it in the second test (with Active = false) and I have some duplicated code.但是在这里我无法在第二个测试中重用它(Active = false)并且我有一些重复的代码。 Is there a way to do it like this or do I need to put the mock code inside the test code?有没有办法做到这一点,还是我需要将模拟代码放在测试代码中?

我找到了如何访问传递的数据。

mockMediatr.Setup(m => m.Send(It.IsAny(), It.IsAny())) .Returns(async (GetBlockedCustomersAndGroupsQuery q, CancellationToken token) => await _getBlockedCustomersAndGroupsHandler.Handle(new GetBlockedCustomersAndGroupsQuery { Active = q.Active}, token));

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

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