RoR rspec未通过<top(required)=“”> </top>中的错误&#39;阻止(2级)测试

时间:2014-08-26 21:14:46

标签: 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

users.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_confirmation属性添加到User模型后出现。那里经过的地方直到那时。我已经继续了这个教程,因为我已经阅读了有关此失败的内容,并将在本教程的后续阶段处理。现在我的测试仍然失败,我不知道为什么。有什么想法吗?

1 个答案:

答案 0 :(得分:6)

堆栈跟踪说:

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类的一个实例。从上下文中,您应该能够推断出它在此上下文中引用了@user变量。

方式方式更强,而不是足以推断出问题的原因的信息:您在第86行调用@user.safe,然后在那里safe没有@user这样的方法。

问题的解决方案您可能打算致电save,而不是safe

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