轨道 3 + FactoryGirl:NameError:未初始化的常量工厂

发布于 2024-10-31 08:14:33 字数 718 浏览 1 评论 0原文

ruby-1.9.2-p180 :007 > Factory.define :user do |user|
ruby-1.9.2-p180 :008 >       user.email                  "[email protected]"
ruby-1.9.2-p180 :009?>     user.password               "foobar"
ruby-1.9.2-p180 :010?>     user.password_confirmation  "foobar"
ruby-1.9.2-p180 :011?>   end
NameError: uninitialized constant Factory

我的 Gemfile:

group :test do 
  gem "rspec-rails"
  gem 'webrat', '0.7.1'
  gem 'spork', '0.9.0.rc4'
  gem 'factory_girl_rails'
end

尽管看起来我已经拥有了应有的一切,但我还是不断收到该错误。我还创建了factories.rb。

谢谢

ruby-1.9.2-p180 :007 > Factory.define :user do |user|
ruby-1.9.2-p180 :008 >       user.email                  "[email protected]"
ruby-1.9.2-p180 :009?>     user.password               "foobar"
ruby-1.9.2-p180 :010?>     user.password_confirmation  "foobar"
ruby-1.9.2-p180 :011?>   end
NameError: uninitialized constant Factory

My Gemfile:

group :test do 
  gem "rspec-rails"
  gem 'webrat', '0.7.1'
  gem 'spork', '0.9.0.rc4'
  gem 'factory_girl_rails'
end

Even tough it seems I have everything as it should, I keep getting that error. I also have factories.rb created.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄人书 2024-11-07 08:14:33

我想你在开发环境的控制台中尝试一下。但是您仅在测试环境中添加 Factory gem。

如果您想在 Gemfile 中使用开发中访问 Factory_girl :

group :test, :development do
  gem 'factory_girl_rails'
end

或者如果您想测试您的工厂,请在测试环境中启动您的控制台:

rails c test

I suppose you try in console in development environment. But you add the Factory gem only in test environment.

If you want access to Factory_girl in development use in your Gemfile :

group :test, :development do
  gem 'factory_girl_rails'
end

Or if you want test your Factory launch your console in test environment :

rails c test
烟─花易冷 2024-11-07 08:14:33

我们也遇到了类似的问题,rake 规范似乎随机失败,并出现错误未初始化常量 FactoryGirl。错误是随机的 ->来来去去。我们回溯了六次 git 提交来尝试解决它。最终这是一个愚蠢的错误。

根本问题是 RAILS_ENV 设置为开发。当您运行 rake spec 时,需要将其设置为 test

地址:

  1. 确保我们在 RAILS_ENV 测试环境中运行 rake 规范,并正确导出/获取其来源。为了永远不会混淆我们的环境,我们修改了 zsh $RPROMPT env 变量以显示当前环境。

    export RPROMPT="[%{$fg_no_bold[yellow]%}$RAILS_ENV%{$reset_color%}]"

  2. 规范 ruby​​ 文件中的 Require FactoryGirl 提供了更好的错误消息。至少 rspec 会运行,而不是在环境错误时以这种方式彻底失败。我们还更新了 gemfile,以确保为开发和测试加载了factory_girl_rails 和factory_girl。

  3. 现在我们只需在专用终端中使用 gem Guard 并设置正确的 RAILS_ENV 来运行 rpsec。

这是那些陷阱之一。

我们正在运行 Ruby 1.9.3、Rails 3.2.9、Rspec 2.12、factory_girl 4.1。

We had a similar problem on our end, rake spec seemed to be randomly failing with the error uninitialized constant FactoryGirl. The error was random -> coming and going. We went back half a dozen git commits to try and resolve it. In the end it was a silly mistake.

The fundamental problem is RAILS_ENV is set to development. It needs to be set to test when you run rake spec.

Address by:

  1. Making certain that we are running rake spec in the RAILS_ENV test environment and its exported/sourced properly. To never confuse our environemnts, we modified zsh $RPROMPT env variable to show the current env.

    export RPROMPT="[%{$fg_no_bold[yellow]%}$RAILS_ENV%{$reset_color%}]"

  2. Require FactoryGirl in the spec ruby files gave a much better error message. At least rspec would run vs just fail outright this way when the environment was wrong. We also updated our gemfile to make sure factory_girl_rails and factory_girl were loaded both for development and testing.

  3. Now we just run rpsec using the gem guard in a dedicated terminal with the proper RAILS_ENV set.

Its one of those gotchas.

We're running Ruby 1.9.3, Rails 3.2.9, Rspec 2.12, factory_girl 4.1.

梦一生花开无言 2024-11-07 08:14:33

我在做 Hartl 教程时也遇到了这个问题。

如果您使用“Spork”来加速测试,并且当您添加 FactoryGirl 代码时它正在后台运行,则您需要停止它并重新启动它。

I also ran into this while I was doing the Hartl tutorial.

If you are using "Spork" to speed up your tests and it is running in the background when you add the FactoryGirl code, you will need to stop it and restart it.

别理我 2024-11-07 08:14:33

您还应该将 Factory.define 更改为 FactoryGirl.define,例如

 FactoryGirl.define do
  factory :user do
    name 'John Doe'
    date_of_birth { 21.years.ago }
  end
end

factory_girl 中的示例文档

You should also change Factory.define to FactoryGirl.define, like in this example

 FactoryGirl.define do
  factory :user do
    name 'John Doe'
    date_of_birth { 21.years.ago }
  end
end

from the factory_girl documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文