ruby-on-rails - RoR rspec 测试失败,错误为 <top (required)> 中的“block (2 levels)”

标签 ruby-on-rails ruby ruby-on-rails-3 rspec

我正在学习 Hartls Rails 教程,但我卡在了第 6 章的末尾。 突然我的一些测试失败并出现相同的错误:

C:\Users\JMA\Projekte\sampleapp>bundle exec rspec spec/
.F......F...FFF....

Failures:

  1) User
     Failure/Error: it { should respond_to(:passwors_confirmation)}
       expected #<User id: nil, name: "Example User", email: "user@example.com",
 created_at: nil, updated_at: nil, password_digest: "$2a$04$OMnlZgC9yWhxbQizs8MG
9uPMCd8LmMlO5MZzkuQyM8it..."> to respond to :passwors_confirmation
     # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'

  2) User when name is not present when email format is valid should be valid
     Failure/Error: expect(@user).to be_valid
       expected #<User id: nil, name: " ", email: "user@foo.COM", created_at: ni
l, updated_at: nil, password_digest: "$2a$04$PtGxi4DjJWFezLFYy1jW5eaI7C/xxyBLXAG
zPepflo8r..."> to be valid, but got errors: Name can't be blank
     # ./spec/models/user_spec.rb:51:in `block (5 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:49:in `each'
     # ./spec/models/user_spec.rb:49:in `block (4 levels) in <top (required)>'

  3) User when name is not present return value of authenticate method with vali
d password
     Failure/Error: before { @user.safe }
     NoMethodError:
       undefined method `safe' for #<User:0x502bc30>
     # ./spec/models/user_spec.rb:86:in `block (4 levels) in <top (required)>'

  4) User when name is not present return value of authenticate method with inva
lid password
     Failure/Error: before { @user.safe }
     NoMethodError:
       undefined method `safe' for #<User:0x50333a0>
     # ./spec/models/user_spec.rb:86:in `block (4 levels) in <top (required)>'

  5) User when name is not present return value of authenticate method with inva
lid password
     Failure/Error: before { @user.safe }
     NoMethodError:
       undefined method `safe' for #<User:0x503b848>
     # ./spec/models/user_spec.rb:86:in `block (4 levels) in <top (required)>'

Finished in 0.52003 seconds
19 examples, 5 failures

Failed examples:

rspec ./spec/models/user_spec.rb:16 # User
rspec ./spec/models/user_spec.rb:47 # User when name is not present when email f
ormat is valid should be valid
rspec ./spec/models/user_spec.rb:90 # User when name is not present return value
 of authenticate method with valid password
rspec ./spec/models/user_spec.rb:96 # User when name is not present return value
 of authenticate method with invalid password
rspec ./spec/models/user_spec.rb:97 # User when name is not present return value
 of authenticate method with invalid password

Randomized with seed 37132

我的 user_specs.rb 看起来像这样:

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(:passwors_confirmation)}

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

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

describe "when email format is invalid" do
    it "should be invalid" do
        adresses = %w[user@foo,com user_at_foo.org example.user@foo. 
            foo@bar_baz.com foo@bar+baz.com]
        adresses.each do |invalid_adress|
            @user.email = invalid_adress
            expect(@user).not_to 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
        expect(@user).to be_valid
        end
    end
end     

describe "when email adress 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     

it { should respond_to(:authenticate) }

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

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

describe "with a password that's to short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
end

describe "return value of authenticate method" do
    before { @user.safe }
    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_inavlid_password) { found_user.authenticate("invalid") }

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

end
end

用户.rb

class User < ActiveRecord::Base
before_save { self.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 }

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

end

在User模型中添加password和password_confirmation属性后出现。直到那时过去的地方。我继续教程是因为我已经阅读了有关此故障的信息,并且将在本教程的稍后阶段进行处理。现在我的测试仍然失败,我不知道为什么。有什么想法吗?

最佳答案

堆栈跟踪显示:

Failure/Error: before { @user.safe }  
NoMethodError:  
  undefined method `safe' for #<User:0x503b848>  
# ./spec/models/user_spec.rb:86:in `block (4 levels) in <top (required)>'  

它告诉你一些关键信息:

  • 问题出现在 user_spec.rb 的第 86 行.
  • 有问题的代码行是before { @user.safe } .
  • 问题是NoMethodError这意味着您正试图在未定义该方法的对象上调用该方法。
  • 它明确告诉你没有方法safe对于 #<User:0x503b848>这是 User 的一个实例类(class)。从上下文中你应该能够推断出它指的是 @user此上下文中的变量。

这是远多的信息,不足以推断问题的原因:您正在调用 @user.safe在第86行,并没有这样的方法safe对于 @user .

该问题的解决方案 可能是您打算调用 save , 不是 safe .

由于其他几个原因,您还有其他一些失败。看看您是否可以阅读错误消息中的信息以了解问题的原因,然后您应该很容易自己弄清楚如何解决这些问题。

关于ruby-on-rails - RoR rspec 测试失败,错误为 <top (required)> 中的“block (2 levels)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25515246/

相关文章:

ruby-on-rails - Windows 下的 Rails 3 readline 问题

ruby-on-rails - 什么时候在 Rails 中使用时间戳?

ruby-on-rails - 六边形架构的缺点

ruby-on-rails - Rails - 装饰器内部验证

ruby - Redmine 安装问题 Ubuntu 12.04

ruby - JetBrains RubyMine 3.2.4 调试器不工作

ruby - docker RUN-不执行脚本

ruby-on-rails - PostgreSQL 总是创建具有偶数 ID 的记录?

ruby-on-rails-3 - 初始化tsearch,Trigram后pg_search multisearch出错

ruby-on-rails - 了解 Rails 路线 : match vs root in routes. rb