Reputation: 1923
Following the newest version of Michael Hartl's rails guide
I have the following test file in my test/models/user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
# prevously.
#@user = User.new(name: "Example Name", email: "[email protected]")
# password and password_conformation is added after has_secure_password is implemented in User.rb
@user = User.new(name: "Example Name", email: "[email protected]", password: "foobar", password_confirmation: "foobar")
end
.... # other tests
# should be a valid user
test "should be valid" do
assert @user.valid?
end
# test for valid email addresses
test "email validation should accept valid addresses" do
valid_addresses = %w[[email protected] [email protected] [email protected]
[email protected] [email protected]]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end
#test for invalid email addresses
test "email validation should reject invalid addresses" do
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
foo@bar_baz.com foo@bar+baz.com]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
end
end
# PROBLEM STARTS HERE !!!!
# test password minimum length as 8
test "password should have a minimum length" do
@user.password = @user.password_confirmation = "a" * 7
assert_not @user.valid?
end
..... # more other tests
end
For some reason it's returning the following when I run bundle exec rake test
1) Failure:
UserTest#test_email_validation_should_accept_valid_addresses [/Users/jackCHH/Desktop/Websites/cloudSolar/test/models/user_test.rb:49]:
"[email protected]" should be valid
2) Failure:
UserTest#test_should_be_valid [/Users/jackCHH/Desktop/Websites/cloudSolar/test/models/user_test.rb:14]:
Failed assertion, no message given.
The problem started after I added the test script to test the password's length. I also added validates :password, length: { minimum: 8 } to my model User.rb. If I remove the validation line in my model and the password_length test script, the test passes again.
My email declaration for @user seems correct ([email protected]), so I am not sure why it's an invalid email now
What am I doing wrong?
Upvotes: 1
Views: 562
Reputation: 1
I was getting the same issue but had
attr_accessor :password, :password_confirmation
in the user.rb file, once I removed this line everything worked without error.
Upvotes: 0
Reputation: 1923
I found the answer.
In my declaration of @user, both my password and password_confirmation are foobar, which is only length 6. I set it so that the requirement is 8 characters minimum, therefore it returns an error.
After changing the password to thisfoobar, the problem is solved.
Upvotes: 2