0

我正在關注Michael Hartl的Ruby on Rails教程。我的測試全部通過,直到我遷移數據庫並添加了最後2次測試。即使我刪除了最近的兩個測試,現在所有的測試都失敗了,但有些東西已經搞亂了。我不確定我是否在遷移或其他方面犯了一些錯誤。RSpec:所有測試都失敗

這是我使用的:

$ bundle exec rake db:migrate 
$ bundle exec rake db:test:prepare 
$ bundle exec rspec spec/ 

這裏的user_spec文件,它完全失敗:

require 'spec_helper' 

describe User do 
    before do 
    @user = User.new(name: "Example User", email: "[email protected]", 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 be_valid } 

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

    describe "when email is not present" do 
    before { @user.email = " "} 
    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[[email protected],com user_at_foo.org [email protected] [email protected]_baz.com [email protected]_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[[email protected] [email protected] [email protected] [email protected]] 
       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 


    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 
end 

這裏是遷移文件:

class AddPassswordDigestToUsers < ActiveRecord::Migration 
    def change 
    add_column :users, :password_digest, :string 
    end 
end 

EDIT2 - 我只是從規範的第一行刪除password: "foobar", password_confirmation: "foobar",現在我只能得到5個錯誤(這是正確的T作爲那些與密碼應該失敗)

EDIT1 - 這裏是失敗的測試

Running all specs 
Running tests with args ["--drb", "-f", "progress", "-r", "/home/niranjan/.rvm/gems/ruby-1.9.3-p194/gems/guard-rspec-1.2.1/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]... 
..FFFFFFFFFFFFFFF......... 

Failures: 

    1) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar") 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:16:in `new' 
    # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>' 

    2) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar") 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:16:in `new' 
    # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>' 

    3) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar") 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:16:in `new' 
    # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>' 

    4) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar") 
    ActiveRecord::UnknownAttributeError: 
     unknown attribute: password 
    # ./spec/models/user_spec.rb:16:in `new' 
    # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>' 

....等等

+0

它究竟如何失敗?粘貼您正在接收的錯誤消息的摘錄。 –

+0

@RenatoZannon請看看我的最新編輯,問題通過刪除'password'和'password_confirmation'消失。但是當我把它們放回去時,爲什麼一切都會失敗? –

+0

它實際上不是問題,故障是預期的,請看看我的迴應。很酷的名字,順便說下:) –

回答

2

從書:

把一切一起給出了代碼清單6.28中的(失敗)測試。我們會讓他們通過第6.3.4節。

測試應該失敗。只要密碼實現完成,他們會在教程後面通過。


現在,解釋爲什麼他們失敗:

你在哪裏創建用戶(您before塊),要傳遞的password屬性的構造線,但該屬性沒有按現在還沒有,所以你的模型抱怨你看到的錯誤。在教程中,此屬性稍後創建。

+0

明白了,非常感謝!我試圖在閱讀前進行測試。欣賞它 –