在Hartls rails教程中获得Rspec测试失败。 6

时间:2013-03-08 22:08:32

标签: ruby-on-rails rspec railstutorial.org

我遇到了这些失败,我不知道在哪里寻找解决问题的方法。我搜索了这个网站,但找不到我的问题。根据Hartl教程,所有测试都应该通过。

失败:

  C:\Sites\rails_projects\sample_app>bundle exec rspec spec
Rack::File headers parameter replaces cache_control after Rack 1.5.
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m
←[32m.←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[32m.←[0m←[32m.←[0m

Failures:

  1) when email address is already taken
     ←[31mFailure/Error:←[0m ←[31muser_with_same_email = @user.dup←[0m
     ←[31mTypeError:←[0m
       ←[31mcan't dup NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:104:in `dup'←[0m
←[36m     # ./spec/models/user_spec.rb:104:in `block (2 levels) in <top (require
d)>'←[0m

  2) when name is too long
     ←[31mFailure/Error:←[0m ←[31mbefore { @user.name = "a" * 51 }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `name=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:76:in `block (2 levels) in <top (required
)>'←[0m

  3) when password is not present
     ←[31mFailure/Error:←[0m ←[31mbefore { @user.password = @user.password_confi
rmation = " " }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `password_confirmation=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:55:in `block (2 levels) in <top (required
)>'←[0m

  4) when password confirmation is nil
     ←[31mFailure/Error:←[0m ←[31mbefore { @user.password_confirmation = nil }←[
0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `password_confirmation=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:65:in `block (2 levels) in <top (required
)>'←[0m

  5) when email format is valid should be valid
     ←[31mFailure/Error:←[0m ←[31m@user.email = valid_address←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `email=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:97:in `block (3 levels) in <top (required
)>'←[0m
←[36m     # ./spec/models/user_spec.rb:96:in `each'←[0m
←[36m     # ./spec/models/user_spec.rb:96:in `block (2 levels) in <top (required
)>'←[0m

  6) when password doesn't match confirmation
     ←[31mFailure/Error:←[0m ←[31mbefore {@user.password_confirmation = "mismatc
h" }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `password_confirmation=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:60:in `block (2 levels) in <top (required
)>'←[0m

  7) when name is not present
     ←[31mFailure/Error:←[0m ←[31mbefore { @user.name = " " }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `name=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:71:in `block (2 levels) in <top (required
)>'←[0m

  8) when email format is invalid should be invalid
     ←[31mFailure/Error:←[0m ←[31m@user.email = invalid_address←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `email=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:87:in `block (3 levels) in <top (required
)>'←[0m
←[36m     # ./spec/models/user_spec.rb:86:in `each'←[0m
←[36m     # ./spec/models/user_spec.rb:86:in `block (2 levels) in <top (required
)>'←[0m

Finished in 2.42 seconds
←[31m30 examples, 8 failures←[0m

Failed examples:

←[31mrspec ./spec/models/user_spec.rb:109←[0m ←[36m# when email address is alrea
dy taken ←[0m
←[31mrspec ./spec/models/user_spec.rb:77←[0m ←[36m# when name is too long ←[0m
←[31mrspec ./spec/models/user_spec.rb:56←[0m ←[36m# when password is not present
 ←[0m
←[31mrspec ./spec/models/user_spec.rb:66←[0m ←[36m# when password confirmation i
s nil ←[0m
←[31mrspec ./spec/models/user_spec.rb:94←[0m ←[36m# when email format is valid s
hould be valid←[0m
←[31mrspec ./spec/models/user_spec.rb:61←[0m ←[36m# when password doesn't match
confirmation ←[0m
←[31mrspec ./spec/models/user_spec.rb:72←[0m ←[36m# when name is not present ←[0
m
←[31mrspec ./spec/models/user_spec.rb:82←[0m ←[36m# when email format is invalid
 should be invalid←[0m

我的user_spec.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(:password_confirmation) }
  it { should respond_to(:authenticate) }

  it { should be_valid }

  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

  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 "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid } 
   end

  describe "when name is too 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

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

User.rb文件:

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

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

可能您可能在describe User之后意外关闭了describe "with invalid password"阻止了两个额外的end

目前:

describe User do

  # ...
  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

应该是:

describe User do

  # ...
  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的数量。

答案 1 :(得分:0)

错误消息告诉我们@user永远不会被实例化

您似乎错过了:each

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

应该是:

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

现在应该制作@user对象&amp;测试变为绿色:)