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

明白了,非常感谢!我试图在阅读前进行测试。欣赏它 –