Email validation fails (Fluent Assertions) - Please help

hi guys
I am new to fluent assertions and really struggling here.

Why is this test failing. Please help. I cant seem to figure out why its failing here, when I run in debug mode, I get

Message=Expected res.Errors {empty} to contain “Email must be a valid email address”.


NUnit.Framework.AssertionException: ‘Expected res.PassedValidation to be true, but found False.’

public void ValidateRequest_InvalidEmail(string email)
var request = GetValidRequest();
request.Email = email;

        var res = _addNewUserAccountRequestValidator.ValidateRequest(request);

        res.Errors.Should().Contain("Invalid Email");

private AddAccountRequest GetValidRequest()
var dept = _fixture.Create();

        var request = _fixture.Build<AddAccountRequest>()
            .With(x => x.DeptId, dept.Id)
            .With(x => x.Email, "")
        return request;

Your test cases are trying to send a string, email, into the test, but your code doesnt use what the test is providing to it to generate the request, it’s using a fixed value (""), which is a valid email, so the test that is expecting an Invalid email instead gets a valid one.

Also your PassedValidation assertion can’t always be True, if you’re sending bad data.

thanks for your explanation, so what do I do ? I am new to fluent assertions and unit testing ???

Here is the extra validation code

  public class AddAccountRequestValidator : IAddAccountRequestValidator
        private readonly AccountBookingContext _context;

        public AddAccountRequestValidator(AccountBookingContext context)
            _context = context;

        public PdrValidationResult ValidateRequest(AddAccountRequest request)
            var result = new PdrValidationResult(true);

            if (MissingRequiredFields(request, ref result))
                return result;

            if (AccountAlreadyInDb(request, ref result))
                return result;

            if (DeptNotFound(request, ref result))
                return result;

            return result;
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.