Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Aimee Knight
9,701 PointsRails Project - 'Writing Tests' Extra Credit
I'm a bit confused while doing the extra credit at the end of this course. I noticed in the 'Validating Presence Video', that calling assert !user.save passes prior to writing any validation.
When doing the extra credit however I created a validation for the length of the status and found that prior to writing any validation assert !status.save fails in that the status is being saved (which makes sense to me).
Why does the status save without any validation but the user does not? What am I missing in my mind?
3 Answers
Jason Seifer
Treehouse Guest TeacherHey Aimee, the discrepancy is due to the type of validation involved and the data sent in. With the user validation, we are sending in a valid profile_name before calling the assert statement. In the status.save failure, there is most likely data on the status that causes the test to fail. I hope that helps! Can you paste your test here so I can confirm?
Aimee Knight
9,701 PointsWhat's happening in my code, is what I also see happening at http://guides.rubyonrails.org/testing.html#running-tests.
test "should not save post without title" do
post = Post.new
assert !post.save, "Saved the post without a title"
end
1) Failure:
test_should_not_save_post_without_title(PostTest) [/test/unit/post_test.rb:6]:
Saved the post without a title.
<false> is not true.
At this point (about 5:19) in the 'Validating Presence' video (the test has been written but that validation has not been) assert !user.save is passing meaning that the user is NOT being saved. In the documentation example the post IS being saved though?
My code which follows the behavior of the documentation is below.
class StatusTest < ActiveSupport::TestCase
test "A status should not be blank" do
status = Status.new
assert !status.save
end
end
class Status < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
end
1) Failure:
test_A_status_should_not_be_blank(StatusTest) [test/unit/status_test.rb:7]:
Failed assertion, no message given.
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
Aimee Knight
9,701 PointsSo, one of my assumptions was that devise was generating some validations automatically.
To test, I removed all the validations that we added in the video to user.rb and tried to 'register' for treebook leaving the entire form blank. Without any of our own validation I still got the message 'Email can't be blank' and 'Password can't be blank'.
However, I can submit a status without having any validation in place.
So, this explains why assert !user.save passes without validation while assert !status.save fails without any validation.
A question still remains though. I've looked through the devise documentation and I see
"Validatable adds the following options to devise_for:
- +email_regexp+: the regular expression used to validate e-mails;
- +password_length+: a range expressing password length. Defaults to 8..128."
Have I come to the correct conclusion? If so, where can I see this file? The devise documentation says it's in: lib/devise/models/validatable.rb, but I don't have a devise folder in my lib directory?