簡體   English   中英

RSpec:所有測試都失敗了

[英]RSpec: All tests failing

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

這是我用過的:

$ 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: "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 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[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


    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個錯誤(這與密碼相關的錯誤是正確的)

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: "user@example.com", 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: "user@example.com", 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: "user@example.com", 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: "user@example.com", 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)>'

.... 等等

從書中:

將所有內容放在一起會產生清單6.28中的(失敗)測試。 我們將讓他們通過第6.3.4節。

測試應該失敗。 一旦密碼實現完成,它們將在稍后的教程中通過。


現在,來解釋他們為什么失敗:

在創建用戶的行(您的before塊)中,您將password屬性傳遞給構造函數,但該屬性尚不存在,因此您的模型會抱怨您看到的錯誤。 在教程中,此屬性稍后創建。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM