RSpec:所有测试都失败了

时间:2012-10-05 23:10:30

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 rspec2 railstutorial.org

我正在关注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)>'

....等等

1 个答案:

答案 0 :(得分:2)

从书中可以看出:

  

将所有内容放在一起会产生代码清单6.28中的(失败)测试。我们将让他们通过第6.3.4节。

测试应该失败。一旦密码实现完成,它们将在稍后的教程中通过。


现在,解释为什么他们失败了:

在您创建用户的行(您的before块)上,您将password属性传递给构造函数,但该属性尚不存在,因此您的模型会抱怨你看到的错误。在教程中,此属性稍后创建。