【问题标题】:Ruby on Rails Tutorial section 6.3 test suite failingRuby on Rails 教程第 6.3 节测试套件失败
【发布时间】:2013-06-17 18:38:02
【问题描述】:

您好,我可以在这方面寻求帮助吗?

我已按照教程从 6.3 升级到 6.3.4。在本章的最后,我似乎无法进行绿色测试。我收到三个错误。我试图理解错误的逻辑,但我似乎无法找到我做错了什么。

我不完全理解的反复出现的错误是 'NoMethodError:undefined method `authenticate' for nil:NilClass'

网上搜了没用。非常感谢您的帮助。

这是 gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.13'
gem 'bootstrap-sass', '2.1'
gem 'bcrypt-ruby', '3.0.1'

group :development, :test do

gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
gem 'guard-rspec', '1.2.1' 
gem 'guard-spork', '1.2.0'
gem 'childprocess', '0.3.6'
gem 'spork', '0.9.2'

end

group :development do
gem 'annotate', '2.5.0'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do

gem 'sass-rails',   '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.2'

group :test do
gem 'capybara', '1.1.2'
gem 'rb-inotify', '0.8.8' 
gem 'libnotify', '0.5.9'  
end

group :production do
gem 'pg', '0.12.2'
end

user_spec 代码

require 'spec_helper'

describe User 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 }

  describe "when name is not present" do
   before { @user.name = "" }
   it { should_not be_valid }

  describe "when email is not present" do
    before { @user.email = "" }
    it { should_not be_valid }

  describe "when name is too long" do
    before { @user.name = "a" * 51 }
    it { should_not be_valid }

  describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user at foo.org example.user@foo. foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not be_valid
      end
     end
    end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address

       # @user.should be_valid => this should be checked!!!
      end      
    end
  end

   describe "when email address is already taken" do
   before do
    user_with_same_email = @user.dup
    user_with_same_email.email = @user.email.upcase
    user_with_same_email.save
   end
   it { should_not be_valid }
end

   describe "when password is not present" do
    before { @user.password = @user.password_confirmation = " " }
    it { should_not be_valid }
    end

   describe "when password doesn't match confirmation" do
  before { @user.password_confirmation = "mismatch" }
  it { should_not be_valid }
end

   describe "when password confirmation is nil" do
   before { @user.password_confirmation = nil }
   it { should_not be_valid }
end

describe "with a password that's too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
  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 == found_user.authenticate(@user.password) }
    end

    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
  end
end 
end 
end
end

这是应用/模型/用户

    class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }

  validates :name, presence: true, length: { maximum: 50 }
 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end

最后是我在运行测试套件时收到的错误:

.................FFF...................

Failures:

  1) User when name is not present when email is not present when name is too long return value of authenticate method with valid password 
     Failure/Error: it { should == found_user.authenticate(@user.password) }
     NoMethodError:
       undefined method `authenticate' for nil:NilClass
     # ./spec/models/user_spec.rb:100:in `block (7 levels) in <top (required)>'

  2) User when name is not present when email is not present when name is too long return value of authenticate method with invalid password 
     Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") }
     NoMethodError:
       undefined method `authenticate' for nil:NilClass
     # ./spec/models/user_spec.rb:104:in `block (7 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:106:in `block (7 levels) in <top (required)>'

  3) User when name is not present when email is not present when name is too long return value of authenticate method with invalid password 
     Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") }
     NoMethodError:
       undefined method `authenticate' for nil:NilClass
     # ./spec/models/user_spec.rb:104:in `block (7 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:107:in `block (7 levels) in <top (required)>'

Finished in 6.03 seconds
39 examples, 3 failures

Failed examples:

rspec ./spec/models/user_spec.rb:100 # User when name is not present when email is not present when name is too long return value of authenticate method with valid password 
rspec ./spec/models/user_spec.rb:106 # User when name is not present when email is not present when name is too long return value of authenticate method with invalid password 
rspec ./spec/models/user_spec.rb:107 # User when name is not present when email is not present when name is too long return value of authenticate method with invalid password 

【问题讨论】:

  • 请说出您尝试了什么以及您遇到问题的部分。
  • 请将其简化为一个具体的、可回答的问题。转储一堆代码并期望我们对其进行整理不是本网站的工作方式。询问特定错误(不是四个错误)或特定测试(不是整个套件)。
  • 您好,感谢您的 cmets。作为一个新手或 ROR 和一般的 web 开发,我发现很难解释错误消息:'NoMethodError:undefined method `authenticate' for nil:NilClass' 我错过了什么明显的东西吗?
  • 这是个好问题。每当您看到“nil:NilClass 的未识别方法 'x'”时,无论调用了 xfound_user 在本例中为 is nil。现在,回到设置 found_user 的位置,并质疑为什么表达式 User.find_by_email(@user.email) 返回 nil 而不是预期的值。
  • 感谢您的评论。我正在考虑它,我想到的唯一答案是,这是因为实际上数据库中没有任何用户的记录,因此也没有任何电子邮件的记录。但我的印象是这是最简单的答案,而不是正确的答案。

标签: ruby-on-rails rspec rspec-rails password-encryption


【解决方案1】:

失败#1 很容易。查看错误消息。你有一个错字。

{ should_not_be_valid } != { should_not be_valid }
            ↑                          ↑

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多