Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moq Setup does not work with "object" as generic type? #1478

Open
iqoOopi opened this issue May 15, 2024 · 1 comment
Open

Moq Setup does not work with "object" as generic type? #1478

iqoOopi opened this issue May 15, 2024 · 1 comment

Comments

@iqoOopi
Copy link

iqoOopi commented May 15, 2024

Moq Version: 4.20.70

I have one MediatR behavior like this:

public class RequestValidationBehaviour<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators) : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
{

    public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
    {
        if (validators.Any())
        {
            var context = new ValidationContext<TRequest>(request);

            var validator1 = validators.First();

            var test = await validator1.ValidateAsync(context, cancellationToken);

            var validationResults = await Task.WhenAll(validators.Select(v => v.ValidateAsync(context, cancellationToken)));
            var failures = validationResults.SelectMany(r => r.Errors).Where(f => f != null).ToList();

            if (failures.Count != 0)
                throw new ValidationException(failures);
        }
        return await next();
    }
}

So I want to unit test it, my unit test is like:

        [Fact]
        public async Task Handle_InvalidRequest_ValidationExceptionThrown()
        {
            // Arrange
            var request = new object();
            var cancellationToken = CancellationToken.None;
            var validatorMock = new Mock<IValidator<object>>();

            validatorMock.Setup(v => v.ValidateAsync(It.IsAny<ValidationContext<object>>(), cancellationToken))
                     .ReturnsAsync(new ValidationResult(new List<ValidationFailure> { new("a", "b") }));

            var validators = new List<IValidator<object>>
                {
                    validatorMock.Object
                };

            var next = new Mock<RequestHandlerDelegate<object>>();
            var behaviour = new RequestValidationBehaviour<object, object>(validators);

            // Act & Assert
            await Assert.ThrowsAsync<Application.Common.Exceptions.ValidationException>(() => behaviour.Handle(request, next.Object, cancellationToken));
            next.Verify(x => x(), Times.Never);
        }
When I run the unit testing, the result is always null instead of the one in Setup call, wondering why?    

image

(updated in the comment, when using object as generic, moq won't work, however changed object to string then everything will work)

Back this issue
Back this issue

@iqoOopi
Copy link
Author

iqoOopi commented May 15, 2024

Interesting, if I modify my var request = new object(); just into var request = "test"; the testing will pass...
image

is that Moq can not handle type "object"?

@iqoOopi iqoOopi changed the title Setup does not work with MediatR behaviour that has a fluent validator Moq Setup does not work with "object" as generic type? May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant