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

Validation not tested. #27

Open
ngbrown opened this issue Feb 21, 2018 · 3 comments
Open

Validation not tested. #27

ngbrown opened this issue Feb 21, 2018 · 3 comments

Comments

@ngbrown
Copy link

ngbrown commented Feb 21, 2018

There are tests for each of the commands, but none of them end up exercising the validation on the commands. That is the FluentValidation or the ComponentModel.DataAnnotation attributes that are on some command properties.

For example, if the following test was added to the ContosoUniversityCore.IntegrationTests.Features.Department.CreateTest class:

[Fact]
public async Task Should_not_create_invalid_department()
{
    var command = new Create.Command
    {
        Budget = 10m,
        Name = "Engineering",
        StartDate = DateTime.Now.Date,
        Administrator = null,
    };

    // really this should fail somehow...
    await SendAsync(command);

    var created = await ExecuteDbContextAsync(db => db.Departments.Where(d => d.Name == command.Name).SingleOrDefaultAsync());

    created.ShouldBeNull();
}

The test would fail because the command's validator rules are not exercised. Specifically this one:

        public class Validator : AbstractValidator<Command>
        {
            public Validator()
            {
                //...
                RuleFor(m => m.Administrator).NotNull();
            }
        }

It seems they are only evaluated on model binding, but no tests exercise that path as it would require spinning up an in-memory ASP.Net server.

How do you normally test the validations? It seems that command validation testing is a central part of the tests that should be in place.

@jbogard
Copy link
Owner

jbogard commented Feb 21, 2018

@ngbrown
Copy link
Author

ngbrown commented Feb 21, 2018

I had read that. I'm not making a comment on the handlers, more on the how to test the validation in the handlers.

I think there are two alternatives; add some controller tests to ensure binding validation runs or modify SliceFixture.SendAsync to also run the validation.

I'm not 100% sure, but you might find you need to add this line to each action in the controllers:

if (!ModelState.IsValid) return this.BadRequest(ModelState);

Edit: That's probably taken care of by the ValidatorActionFilter but I don't see where it is tested.

@jbogard
Copy link
Owner

jbogard commented Feb 21, 2018 via email

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

2 participants