Reputation: 1280
I'm currently following Michael Hartl's ROR book to create a web application. The following is the code from Chapter 7.
Listing 7.21: test/integration/users_signup_test.rb
require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
test "invalid signup information" do
get signup_path
assert_no_difference 'User.count' do
post users_path, user: { name: "",
email: "user@invalid",
password: "foo",
password_confirmation: "bar" }
end
assert_template 'users/new'
end
end
It is to test the validity of a user signup form. I'm a bit confused about why the values for name, email, password and password_confirmation are filled this way? (meaning, for example, why the value of email is user@invalid; why the password's value is foo?)
Thank you very much and I'm looking forward to your answer!
Upvotes: 0
Views: 241
Reputation: 787
As per Chapter 7 from Ruby-on-Rails Tutorial, Micheal is trying to explain in that section how there should be validations on our signup page and make it clear to our application of what to accept from user and what NOT to accept from user.
Now your question is why the email is filled as user@invalid. This is because we want to test the validations that we have applied on signup page. According to which an email is only valid if the format is something like [email protected]; this makes the application understand the valid format submission.
Similarly for password : foo, you can see that we have two values that are supposed to match which are password and password_confirmation. And in the above code, they both don't match so again as I explained it makes the application understand that this is not a valid submission.
And lastly, as you can see the code, these values are under
test "invalid signup information" do
which makes us understand that show message invalid signup information if the information is as below, i.e.:
name: "",
email: "user@invalid",
password: "foo",
password_confirmation: "bar"
All of these are non acceptable information.
Upvotes: 0
Reputation: 1775
I am agree with toddmetheny answer in fact you can modify the test as you want for example
require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
test "invalid signup information" do
get signup_path # go to the sign up path
assert_no_difference 'User.count' do # This assertion is checking that "username" should not be inserted in User table if the password and password_confirmation is not same
post users_path, user: { name: "username",
email: "user@invalid",
password: "123456",
password_confirmation: "123" }
end
assert_template 'users/new' # This assertion is checking that after invalid password user should go to users/new template
end
end
Hope that i make it more clear
Upvotes: 3
Reputation: 4453
You're testing invalid signup information to make sure a user is not created. You're performing a http post request to the users create action and you're sending the data in that users hash. Foo
and bar
are arbitrarily chosen values mainly significant because they don't match as the password and confirmation are required to.
Upvotes: 3