mercredi 9 novembre 2016

Ruby on Rails Error: Expected true to be nil or false

I am studying Ruby on Rails Tutorial by Michael Hartl. I am working on my user's name and email testing.

Here is my user_test.rb:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(name: "Example User", email: "user@example.com")
  end

  test "should be valid" do
    assert @user.valid? # succeds if @user.valid? returns true else returns false
  end

  test "name should be present" do
    @user.name = ""   # returns true if a user name is not a blank string.
    assert_not @user.valid?
  end

  test "email should be present" do
    @user.email = ""   # returns true if a user email is not a blank string.
    assert_not @user.valid?
  end
end

Here is my user.rb:

class User < ApplicationRecord
  validates :name, presence: true   # check name. Go to user_test.rb  line 15
  validates :email, presence: true  #    //              //           line 
end

I am supposed to get green when i run bundle exec rake test

However, I am getting this error instead:

 Failure:
UserTest#test_email_should_be_present [/Users/YOUNGSOKIM/workspace/prac_app/test/models/user_test.rb:21]:
Expected true to be nil or false

Aucun commentaire:

Enregistrer un commentaire