Rails教程验证问题

时间:2014-06-13 17:37:38

标签: ruby-on-rails ruby unit-testing ruby-on-rails-4

我正在关注Rails教程建模用户章节:http://www.railstutorial.org/book/modeling_users#cha-modeling_users

我的user.rb看起来像:

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  has_secure_password
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:   true,
                format:     { with: VALID_EMAIL_REGEX },
                uniqueness: { case_sensitive: false }

  validates :password, length: { minimum: 6 }
end

我的用户模型规范如下:

describe User, :type => :model do

   before do
     @user = User.new(name: "Example User", email: "user@example.com",
                 password: "foobar", password_confirmation: "foobar")
   end

   subject { @user }

   it { should respond_to(:name) }
   it { should respond_to(:email) }
   it { should respond_to(:password_digest) }
   it { should respond_to(:password) }
   it { should respond_to(:password_confirmation) }
   it { should respond_to(:authenticate) }

   it { should be_valid }

   ... (other methods are here) 

describe "when password is not present" do
   before do
     @user = User.new(name: "Example User", email: "user@example.com",
                   password: "foobar", password_confirmation: "foobar")
   end
   it { should_not be_valid }
end
describe "return value of authenticate method" do
   before { @user.save }
   let(:found_user) { User.find_by(email: @user.email) }

   describe "with valid password" do
      it { should eq found_user.authenticate(@user.password) }
   end

   describe "with invalid password" do
     let(:user_for_invalid_password) { found_user.authenticate("invalid") }

     it { should_not eq user_for_invalid_password }
     specify { expect(user_for_invalid_password).to be_false }
   end
end

我非常确定这与Rails教程代码完全相同,但我得到以下失败的测试错误:

rspec ./spec/models/user_spec.rb:83 # User when password is not present should not be valid
rspec ./spec/models/user_spec.rb:108 # User return value of authenticate method with invalid password should be false

2 个答案:

答案 0 :(得分:2)

我通过在GitHub上查看Ruby on Rails教程书的源代码(Rails 4)来检查这一点:spec/models/user_spec.rb。根据那里的代码,看起来您的密码目前是可接受的类型,这就是您的测试失败的原因。我的意思是你的密码很好。 foob​​ar是一个有效的密码。下面的空字符串将传递给User模型验证。

  describe "when password is not present" do
    before do
      @user = User.new(name: "Example User", email: "user@example.com",
                       password: " ", password_confirmation: " ")
    end
    it { should_not be_valid }
  end

第二个我不确定,但如果您尝试使用Rails 3 spec/models/user_spec.rb相关代码进行同一测试,它会有所帮助:

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not == user_for_invalid_password }
      specify { user_for_invalid_password.should be_false }
    end

它看起来略有不同,但它正在测试同样的事情。这只是建议,因为我不确定出了什么问题。

答案 1 :(得分:0)

看起来缺少几种方法(如果您正在尝试完全匹配教程)清单6.25&amp; 6.28:

描述&#34;当密码不匹配确认&#34;做     在{@ user.password_confirmation =&#34; mismatch&#34;之前}     它{should_not be_valid}   端

描述&#34;密码太短&#34;做     在{@ user.password = @ user.password_confirmation =&#34; a&#34;之前* 5}     它{应该是_无效}   端