【问题标题】:Testing the User Model with Rspec, Devise, and Factory Girl使用 Rspec、Devise 和 Factory Girl 测试用户模型
【发布时间】:2011-08-27 06:58:11
【问题描述】:

我认为我的用户工厂正在构建中存在问题。我收到一条错误消息,提示密码不能为空,但它已明确设置在我的 factory.rb 中。有人看到我可能遗漏的任何东西吗?还是规范失败的原因?我为我的其他模型之一做了非常相似的事情,而且似乎很成功。我不确定这个错误是否是由设计引起的。

Rspec 错误

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

工厂.rb

Factory.define :user do |user|
  user.name                   "Test User"
  user.email                  "user@example.com"
  user.password               "password"
  user.password_confirmation  "password"
end

user_spec.rb

require 'spec_helper'

describe User do
  before(:each) do
    @user = Factory.build(:user)
  end

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

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

【问题讨论】:

    标签: ruby-on-rails ruby rspec devise factory-bot


    【解决方案1】:

    在 Factory Girl 中,这会创建属性:

    @user_attr = Factory.attributes_for(:user)
    

    这会创建一个新实例:

    @user = Factory(:user)
    

    所以改变上面的并尝试:

    User.create!(@user_attr)
    

    深入来说,您尝试做的事情失败了,因为:

    • 您正在创建一个未保存的新实例

    • 密码是虚拟属性

    • 实例的属性不包含虚拟属性(我猜)

    【讨论】:

    • 不幸的是,这似乎不起作用。我确实想构建属性,以便我可以使用@user.attributes.merge(:name => "Whatever") 轻松操作@user 以进行未来测试。这就是我尝试 User.create!(@user.attributes) 的原因。我尝试做 User.create!(@user) 但失败了,这一次说电子邮件是空白的,密码是空白的。
    • 那行得通。这很有趣。我想现在让我感到困惑的是,我不确定为什么我的另一个似乎工作。我有一个“角色”模型,我做同样的事情,使用@role = Factory.build(:role) 和 Role.create!(@role.attributes) 它似乎通过了。有趣的。谢谢先生。
    • 您的测试因虚拟属性而失败
    • 不,它们只是达到目的的手段:基本上,您的模型中没有 password_confirmation 列。请参阅此处了解更多信息:railscasts.com/episodes/16-virtual-attributes
    • 感谢 apneadiving,感谢您的帮助。
    【解决方案2】:

    IMO 最简单的方法:

    FactoryGirl.modify do
      factory :user do
        after(:build) { |u| u.password_confirmation = u.password = ... }
      end
    end
    

    【讨论】:

      【解决方案3】:

      一个对我有用的小贴士。我正在使用FactoryGirl.create(:user),但它不起作用。改成这样:

      user = FactoryGirl.build(:user)
      user.password = "123456"
      user.save
      post :login, {:email => user.email, :password => "123456"}
      # do other stuff with logged in user
      

      这可能是因为“密码”是一个虚拟字段。希望这可以暗示某人。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多