使用 Test::Unit 和 Shoulda 设置 Factory Girl
我正在尝试在 Ruby on Rails 中使用 Test::Unit 和 Shoulda 设置 Factory Girl。 我已经安装了 gem,在 test/factories 目录下创建了工厂文件,并在 test/models 目录下创建了 spec 文件。 我收到的当前错误是“ArgumentError:没有这样的工厂:测试”,这使我相信 test_factory.rb 文件没有被加载? 知道我应该改变什么吗?
这是我的文件。
#test/factories/test_factory.rb
Factory.define :test do |t|
t.name 'test_spotlight'
t.label 'test spotlight label'
end
和
#test/modes/test_spec.rb
require 'test_helper'
require 'factory_girl'
class TestTest < Test::Unit::TestCase
def setup
@test = Factory.build(:test)
end
context "A test" do
should "save with the minimum requirements" do
assert @test.save
end
end
end
I'm trying to set up Factory Girl with Test::Unit and Shoulda in Ruby on Rails. I have installed the gem, created my factory file under the test/factories directory, and created my spec file under the test/models directory. The current error I'm getting is 'ArgumentError: No such factory: test', which leads me to believe that the test_factory.rb file is not being loaded? Any idea as to what I should change?
Here are my files.
#test/factories/test_factory.rb
Factory.define :test do |t|
t.name 'test_spotlight'
t.label 'test spotlight label'
end
and
#test/modes/test_spec.rb
require 'test_helper'
require 'factory_girl'
class TestTest < Test::Unit::TestCase
def setup
@test = Factory.build(:test)
end
context "A test" do
should "save with the minimum requirements" do
assert @test.save
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我在我的一个项目中也遇到了这个问题。 我不确定到底是什么导致初始化代码被跳过,但您可以像这样强制加载工厂定义:
希望这会有所帮助。
I've run into this problem on one of my projects too. I'm not sure precisely what's causing the initialization code to be skipped but you can force load the factory definitions like this:
Hope this helps.
尝试将其放入 test_helper.rb 中:
Try putting this in test_helper.rb:
刚刚发现factory_girl_rails,它说自动加载是它唯一的额外功能 https://github.com/thoughtbot/factory_girl_rails
Just discovered factory_girl_rails, where it says the autoloading is the only extra thing it has https://github.com/thoughtbot/factory_girl_rails
我有同样的问题。 最终,我将所有工厂放入“/test/factories.rb”中,并在“/test/test_helper.rb”文件中写入以下几行:
您可以通过在 test_helper 中要求多个文件来对它们执行相同的操作。 我还没有弄清楚为什么“factory_girl's”自述文件中提到的自动包含没有发生。
I had the same problem. Eventually I made do by putting all my factories in "/test/factories.rb" and writing the following lines in my "/test/test_helper.rb" file:
you could do the same thing for multiple files by requiring them in the test_helper. I haven't yet figured out why the auto-include that's mentioned in "factory_girl's" readme doesn't happen.
我还设法通过将这一行放入我的环境中来解决这个问题。rb:
config.gem "factory_girl", :source => “http://gemcutter.org”
还要确保您拥有最新的 gem:
名称从“thoughtbot-factory_girl”更改为“factory_girl”,源从“http://gems.github.com" 到 "http:// /gemcutter.org”。
I also managed to get rid of this issue by putting this line into my environment.rb:
config.gem "factory_girl", :source => "http://gemcutter.org"
Make also sure you have the latest gem:
Name changed from "thoughtbot-factory_girl" to "factory_girl", source changed from "http://gems.github.com" to "http://gemcutter.org".
如果您在使用 ruby 1.9.2 时遇到此问题,require 需要扩展路径。
这个补丁解决了我的问题。 我刚刚发送了一个请求请求。
之后,您可以将其添加到您的
test_helper.rb
中:In case you had this issue with ruby 1.9.2, require expects the expanded path.
This patch solved my problem. I just sent a pull request.
After that you can add this to your
test_helper.rb
:如果我只需要在 test_helper.rb 中使用“factory_girl”,我会得到与您提到的相同的行为,但如果我在 config/test/environment.rb 中需要它(请注意,我使用 environmentalist)它将正确找到工厂定义,没有任何问题。
我在阅读了 Factory Girl rdoc 后尝试了此操作,其中提到将 config.gem 放入您的环境中。
If I only required 'factory_girl' in test_helper.rb, I would get the same behavior you mentioned, yet if I required it in my config/test/environment.rb (note I use environmentalist) it would properly find the Factory definition without any issue.
I tried this after reading the factory girl rdoc where it says to put config.gem in your environment.
我还遇到了问题 - 将 FactoryGirl 更新到 1.3.2 后 - 来自测试/工厂的工厂不再自动加载。
我可以通过将 dg 中的代码添加到 test_helper.rb 中来解决这个问题:
在 Textmate 中运行单个测试时,一切正常,但使用 rake test:units 从命令行运行所有单元测试失败,并出现 DuplicateDefinitionError (I读到它可能与 ruby 1.8.x 有关)。 所以我稍微改变了代码:
I also ran into the problem - after updating FactoryGirl to 1.3.2 - that factories from test/factories were not loaded automatically any more.
I could get rid of the problem by adding the code from dg into test_helper.rb:
When running single tests in Textmate, everything worked fine, but running e.g. all unit tests from the command line using rake test:units failed with a DuplicateDefinitionError (I read that it has probably something to do with ruby 1.8.x). So I slightly changed the code:
您是否尝试过将其移动
到您的 test/test_helper.rb ?
工厂自动加载机制可能取决于调用需求的位置。 它可能试图找到工厂 *test/models/factories/** 而不是 *test/factories/**
Have you tried moving the
to your test/test_helper.rb ?
The factory auto-loading mechanism may depend on where the the require is called. It could be trying to find factories *test/models/factories/** instead of *test/factories/**
不要将工厂文件命名为 test_factory.rb,而是尝试将其命名为factory.rb
Instead of naming your factory file test_factory.rb, try naming it factory.rb
有趣的。 我在尝试让 Cucumber 与 Factory_girl 一起工作时遇到了类似的问题。 我最初配置了要查找的factory_girl('config.gem'),但未在cucumber环境中加载,并且在'features/support/env.rb'中完全需要,与cucumber对webrat等所做的相同。这才开始当我明确告诉factory_girl按照上面肯尼的建议找到它的定义时,它就起作用了。
当我从 env.rb 中删除 require 语句并在 Cucumber 环境中完全需要factory_girl时,效果消失了,factory_girl开箱即用。
因此,这似乎确实是一个何时(或在何种情况下)加载factory_girl 的问题。
Interesting. I had a similar problem trying to get cucumber to work with factory_girl. I had originally configured factory_girl to be looked for ('config.gem') but not loaded in the cucumber environment and fully required in 'features/support/env.rb', same as cucumber does for webrat, etc. That only started to work when I explicitly told factory_girl to find its definitions as Kenny suggested above.
When I removed the require statement from env.rb and fully required factory_girl in the cucumber environment, the effect went away and factory_girl worked out of the box.
So it really seems to be a question of when (or in which context) factory_girl gets loaded.
由于不同的人使用不同版本的 Rails(现在最常见的是 2.x 和 3.x),因此包含环境的其他相关部分非常重要(最重要的是您使用的是哪个版本的 Rails) 。 从 factory_girl 网页,版本 1.3.0 文档 (http://rubydoc.info/gems/factory_girl/1.3.0/frames):
如果您在加载时遇到问题,我建议您确保使用正确的版本。 我假设,factory_girl 大于 1.2.4 的版本是作为“factory_girl_rails”(Rails 3.0+)gem 的依赖项引入的。
Since different people are using different versions of Rails (2.x and 3.x being the most common now), it is important to include the other pertinent pieces of your environment (the most important being which version of Rails you're on). From the factory_girl web page, version 1.3.0 documentation (http://rubydoc.info/gems/factory_girl/1.3.0/frames):
If you're having trouble with the loading, I'd suggest making sure that you are using the right version. The versions of factory_girl greater than 1.2.4, I'd assume, are brought in as dependencies for the 'factory_girl_rails' (Rails 3.0+) gem.
我添加了
需要“factory_girl”
到spec_helper.rb 这有帮助,但后来我想起 Spork 有时会出现一些问题,所以我在没有需要 File.dirname(FILE) + "/factories"
require
的情况下重新启动了 Spork,然后它工作得很好。I added
require 'factory_girl'
to spec_helper.rb whiche helped, but then I rememberd Spork can sometimes ba a bit of a problem, so I restarted Spork without therequire File.dirname(FILE) + "/factories"
require
and then it worked fine.