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

NUnit.Framework.AssertionException
HResult=0x80131500
Message=Expected res.Errors {empty} to contain “Email must be a valid email address”.

and

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

[TestCase(“user@”)]
[TestCase(“@”)]
[TestCase(“user”)]
[TestCase(null)]
[TestCase(“”)]
public void ValidateRequest_InvalidEmail(string email)
{
//arrange
var request = GetValidRequest();
request.Email = email;

        //act
        var res = _addNewUserAccountRequestValidator.ValidateRequest(request);

        //assert
        res.PassedValidation.Should().BeTrue();
        res.Errors.Should().Contain("Invalid Email");
    }

private AddAccountRequest GetValidRequest()
{
var dept = _fixture.Create();
_context.Dept.Add(dept);
_context.SaveChanges();

        var request = _fixture.Build<AddAccountRequest>()
            .With(x => x.DeptId, dept.Id)
            .With(x => x.Email, "user@domain.com")
            .Create();
        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 ("user@domain.com"), 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.