【问题标题】:Defining FactoryGirl for User model of Devise fails为 Devise 的用户模型定义 FactoryGirl 失败
【发布时间】:2012-08-27 15:42:44
【问题描述】:

过去两个小时我一直在找出问题所在,但在任何地方都找不到答案。

这是我的第一个 Rails 应用程序(Hartl 的教程除外),所以解决方案可能很简单。我正在使用 Devise 来管理我的用户,到目前为止一切都很好。

为了测试用户模型,我定义了一个这样的工厂:

FactoryGirl.define do
  factory :user do
    email "g@g.com"
    password "123123"
    password_confirmation { "123123" }
  end
end

测试是:

describe User do
#  pending "add some examples to (or delete) #{__FILE__}"

    @user = FactoryGirl.create(:user) 


    subject(:user)

    it { should respond_to(:email) }
    it { should respond_to(:password) }

    it { should be_valid }
end

但最后一行(it { should be_valid } )未通过测试。

我已经打印了 user/@user 的值(都试过了),结果为零。 编辑:它不是零。它的

#<User id: 13, email: "email1@factory.com", encrypted_password: "$2a$04$.lWs6yadJu/Ya67xi.W1F.fd6sWLGkzc/59.lgTi0sA7...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2012-08-27 15:48:23", updated_at: "2012-08-27 15:48:23">

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :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
  # attr_accessible :title, :body

  validates :email, :presence => true
  validates :password, :presence => true
end

什么是我看不到的?

【问题讨论】:

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


    【解决方案1】:

    好的,我终于找到了问题所在。事实证明,当您对 User 模型进行更改时,您必须重新启动“spork”,因为它会预先加载它。

    【讨论】:

    • 您也可以尝试在 config/environments/test.rb 中按照 guides.rubyonrails.org/configuring.html 设置 config.cache_classes = false - 应该可以避免每次制作时都必须重新启动 spork改变。
    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 2021-06-02
    • 1970-01-01
    • 2020-08-22
    • 2015-02-18
    相关资源
    最近更新 更多