2

我正在编写一个用户模型,而 RSpec 坚持我将字段留空,事实上,这些字段填充了一个完全有效的密码。这是我的 spec/models/user_spec.rb 文件:

require 'spec_helper'

describe User do
    before(:each) do
        @attr = {
            :name => "Example User",
            :email => "user@example.com",
            :password => "password",
            :password_confirmation => "password"
        }
    end

    it "should create a new instance given valid attributes" do
        User.create!(@attr)
    end

    it "should require a name" do
        no_name_user = User.new(@attr.merge(:name => ""))
        no_name_user.should_not be_valid
    end

    it "should reject names that are too long" do
        long_name = "a" * 51
        long_name_user = User.new(@attr.merge(:name => long_name))
        long_name_user.should_not be_valid
    end

    it "should accept valid email addresses" do
        addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
        addresses.each do |address|
            valid_email_user = User.new(@attr.merge(:email => address))
            valid_email_user.should be_valid
        end
    end

    it "should reject invalid email addresses" do
        addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
        addresses.each do |address|
            invalid_email_user = User.new(@attr.merge(:email => address))
            invalid_email_user.should_not be_valid
        end
    end

    it "should reject duplicate email addresses" do
        User.create!(@attr)
        user_with_duplicate_email = User.new(@attr)
        user_with_duplicate_email.should_not be_valid
    end

    it "should reject email addresses identical up to case" do
        upcased_email = @attr[:email].upcase
        User.create!(@attr.merge(:email => upcased_email))
        user_with_duplicate_email = User.new(@attr)
        user_with_duplicate_email.should_not be_valid
    end

    describe "password validations" do
        it "should require a password" do
            User.new(@attr.merge(:password => "", :password_confirmation => "")).should_not be_valid
        end

        it "should require a matching password confirmation" do
            User.new(@attr.merge(:password_confirmation => "invalid")).should_not be_valid
        end

        it "should reject short passwords" do
            short = "a" * 5
            hash = @attr.merge(:password => short, :password_confirmation => short)
            User.new(hash).should_not be_valid
        end

        it "should reject long passwords" do
            long = "a" * 41
            hash = @attr.merge(:password => long, :password_confirmation => long)
            User.new(hash).should_not be_valid
        end
    end

    describe "password encryption" do
        before(:each) do
            @user = User.create!(@attr.merge(:password => "foobar", :password_confirmation => "foobar"))
        end

        it "should have an encrypted password attribute" do
            @user.should respond_to(:encrypted_password)
        end
    end
end

这是我的 app/models/user.rb 文件:

class User < ActiveRecord::Base
    attr_accessible :name, :email
    attr_accessor :password

    email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

    validates(:name, :presence => true,
        :length => { :maximum => 50 })

    validates(:email, :presence => true,
        :format => { :with => email_regex },
        :uniqueness => { :case_sensitive => false })

    validates(:password, :presence => true,
        :confirmation => true,
        :length => { :within => 5..41 })
end

运行 RSpec 后,我收到以下错误:

  1) User should create a new instance given valid attributes
     Failure/Error: User.create!(@attr)
     ActiveRecord::RecordInvalid:
       Validation failed: Password can't be blank
     # ./spec/models/user_spec.rb:25:in `block (2 levels) in <top (required)>'

  2) User should accept valid email addresses
     Failure/Error: valid_email_user.should be_valid
       expected #<User id: nil, name: "Example User", email: "user@foo.com", created_at: nil, updated_at: nil, encrypted_password: nil> to be valid, but got errors: Password can't be blank
     # ./spec/models/user_spec.rb:43:in `block (3 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:41:in `each'
     # ./spec/models/user_spec.rb:41:in `block (2 levels) in <top (required)>'

  3) User should reject duplicate email addresses
     Failure/Error: User.create!(@attr)
     ActiveRecord::RecordInvalid:
       Validation failed: Password can't be blank
     # ./spec/models/user_spec.rb:56:in `block (2 levels) in <top (required)>'

  4) User should reject email addresses identical up to case
     Failure/Error: User.create!(@attr.merge(:email => upcased_email))
     ActiveRecord::RecordInvalid:
       Validation failed: Password can't be blank
     # ./spec/models/user_spec.rb:63:in `block (2 levels) in <top (required)>'

  5) User password encryption should have an encrypted password attribute
     Failure/Error: @user = User.create!(@attr.merge(:password => "foobar", :password_confirmation => "foobar"))
     ActiveRecord::RecordInvalid:
       Validation failed: Password can't be blank
 # ./spec/models/user_spec.rb:92:in `block (3 levels) in <top (required)>'

其中每一个的问题是密码字段不是空白的!它填充了“密码”这个词——它很好地介于 5 和 41 之间。在某些情况下,我将它合并到那个非常具体的测试的属性中。

谁能解释为什么这些测试失败了?

4

1 回答 1

7

我不知道确切的问题,但我可以教你如何调试它。

第 1 步:在测试环境中打开 Rails 控制台。

$> rails console test

这使您可以像在测试规范中一样执行代码。我不确定您是否熟悉环境,但无论如何这里是一篇好文章。

第 2 步:选择最容易修复的故障。在这种情况下,它似乎是User should create a new instance given valid attributes

第 3 步:在测试控制台中逐行输入您希望为失败的规范执行的所有代码。

>> @attr = {
        :name => "Example User",
        :email => "user@example.com",
        :password => "password",
        :password_confirmation => "password"
    }
>> User.create!(@attr)

第 4 步:如果您无法重现故障,则说明您的设置有其他问题。看看里面spec_helper.rb。也许你忘了跑rake db:migrate或者rake db:test:prepare?当您使用更高级的工具(如Zeus )时,这一点变得更加重要。修理它。

第 5 步:如果您能够重现故障,是的!仔细检查打印的错误消息。正如@AmitKumarGupta 提到的,这可能是因为password不可批量分配。这是一篇关于这意味着什么的好文章。尝试各种创建user. 例如,

>> user = User.new @attr
>> user.valid?  # should return true, but if it is false ...
>> user.errors  # is there an error for password and password confirmable?
>> user.inspect # maybe some rouge code deleted the password by accident?

第 6 步:希望现在您已经找到了解决方案。添加解决方案并重新运行规范。现在从第 1 步开始重复,直到全部通过。

边注

我强烈推荐FactoryGirlShoulda。这是should have_a_valid_factory的要点。

更新

如果您按照我上面的说明执行以下操作

>> u = User.new @attr

您将看到以下错误消息:

WARNING: Can't mass-assign protected attributes for User: password, password_confirmation

我上面链接的文章将解释这意味着什么。使用 FactoryGirl 将解决您的问题,或者您可以使用

user = User.new @attr # without password, password confirmation
user.password = 'password'
user.password_confirmation = 'password'
user.save!
于 2013-07-14T03:21:44.873 回答