0

I have the following regex expression to validate emails VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

I have also the following test function

test "valid signup information" do
    get signup_path
    assert_difference 'User.count', 1 do
      post users_path, params: { user: { first_name: "John",
                                         last_name: "Doe",
                                         email: "[email protected]",
                                         password: "foobar123",
                                         password_confirmation: "foobar123" } }
    end
    follow_redirect!
    assert_template 'users/show'
    assert is_logged_in?
end

Yet, the test fails only when the email is set to [email protected] Every single other mail of the same format works. I can't see why. Moreover, when I try to manually sign up, the [email protected] address works perfectly fine.

Here's the log message when testing with [email protected]

Running via Spring preloader in process 5405
Started with run options --seed 28654

 FAIL["test_valid_signup_information", UsersSignupTest, 1.127243652001198]
 test_valid_signup_information#UsersSignupTest (1.13s)
        "User.count" didn't change by 1.
        Expected: 2
          Actual: 1
        test/integration/users_signup_test.rb:19:in `block in <class:UsersSignupTest>'

  20/20: [=============================================================================================================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.14680s

Thanks,

3
  • Maybe your test method registers the user in a place where that user already exists and doesn't raise usercount. Commented Aug 27, 2017 at 21:04
  • @linden2015 Really unlikely. How can I check the database for every single environments (prod, test, dev?) Thanks Commented Aug 27, 2017 at 21:20
  • I don't think your regex is the problem: rubular.com/r/SC4XTmOwoe Commented Aug 27, 2017 at 21:32

2 Answers 2

1

The regex is fine, you can check it by doing in terminal:

> '[email protected]' =~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
=> 0

The failure only says the number of users didn't change. So it could be anything really. After running your tests open console with rails c test and try to create the failed user manually. You will see why it doesn't get created in your test environment.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I have tried to reset my database in every single environment. When I launch the rails console, User.count returns 0 for development and test environment. Yet, when I launch the test, I get the same error "User.count" didn't change by 1. Expected: 2 Actual: 1 How is possible to have 1 record although in the console, it says 0?
Yes, it was because of a fixture used in another test! Thanks
0

Your regex pattern isn't so great.

  • It matches: [email protected]
  • It doesn't match: a_b@d_e.com
  • If you put the - at the end or beginning of the character class, then you don't need to escape it. [\w+.-]
  • For readability: ^ and $ are far more commonly used.

Email pattern matching is a hard problem. I suggest googling it instead of coming up with your own pattern. Or make the pattern only catch basic errors and emailing the address will tell you if it's valid or not.

Also, your email pattern should have it's own test, to isolate it's proper working. Then when you test a signup method you're only testing the code belonging to that.

2 Comments

Do note, that ^ and $ are not the same thing as \A and \z. ^ matches the start of a line, while \A matches the start of a string and $ matches the end of a line, while \z matches the end of the string
Right. Absolute ends. I suppose if you run a find(pattern) call, that BAD DATA \n VALID EMAIL might get through.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.