Reputation: 125
Im following along Hartl's RoR tutorial and came across the following test code in ch.6, which is not making sense:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User", email: "[email protected]")
end
test "should be valid" do
assert @user.valid?
end
test "name should be present" do
@user.name = " "
assert_not @user.valid?
end
end
Focusing on the last test "name should be present", the @user.name variable is being assigned blank spaces. So wont that make the test fail, no matter what?
Secondly, what is assert_not? Assert is something like "check if this is true". So assert_not is "check if its false"?
So the above test is doing the following?
@name is made to be blank spaces
check if @name is invalid?
Yes it is invalid because @name was made to blank spaces in step 1.
So confused....
Upvotes: 0
Views: 30
Reputation: 49910
Object#present? is defined in rails as "not blank" - - and String#blank? is defined as "empty or contains whitespaces only". Therefore the username filled in with all spaces would not be present. assert_not is the opposite of assert - it requires the expression to be false. So assuming your User object is validating presence of the name attribute, then filling it in with spaces will make the name not present, therefore making the object not valid, and the assert_not should pass
Upvotes: 2