【问题标题】:FactoryGirl uninitialized constantFactoryGirl 未初始化常量
【发布时间】:2016-01-03 10:31:35
【问题描述】:

我正在努力解决 FactoryGirl 的未初始化常量错误

 NameError: uninitialized constant Usernotactivated

我的 factory.rb 文件中有以下内容

FactoryGirl.define do
  factory :usernotactivated do
  name "foonotactiv"
  email "foonotactiv@example.com"
  password "secretnot"
  activated false
end
end

以及我的规范中的以下内容

it "should redirect to activation alert when it signs me in with an inactivated account" do
user = FactoryGirl.build(:usernotactivated)
visit login_path
  fill_in 'Email', :with => usernotactivated.email
  fill_in 'Password', :with => usernotactivated.password
click_button 'Sign in'
expect(page).to have_content 'your account is not activated'
end

在两个文件 rails_helper.rb 和 spec_helper.rb 我都添加了这些行:

require 'factory_girl_rails'

我也试过

require 'factory_girl'

不知何故,我将“usernotactivated”更改为“user”我没有收到此未初始化的常量错误,但我不确定是否是因为“user”可能是受保护的名称。谁能给我提示我应该在哪里调查这个问题?您能否告诉我 rails_helper 文件(我在所有规范中都需要)和 spec_helper 文件之间的区别?谢谢。

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    FactoryGirl 构建模型的实例 - 默认情况下,名为 :usernotactivated 的工厂将希望构建 Usernotactivated 模型的实例。您的应用中没有此模型,因此您会收到错误消息。

    如果该工厂应该生成您的 User 模型的实例,您可以将工厂重命名为 :user,或者将类名选项添加到您的工厂:

    factory :user_not_activated, class: User do
    

    【讨论】:

    • API 已更改,语法现在为factory :user_not_activated, class: User do
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多