【问题标题】:Setup Factory Girl with Test::Unit and Shoulda使用 Test::Unit 和 Shoulda 设置 Factory Girl
【发布时间】:2009-07-21 15:38:18
【问题描述】:

我正在尝试在 Ruby on Rails 中使用 Test::Unit 和 Shoulda 设置 Factory Girl。我已经安装了 gem,在 test/factories 目录下创建了我的工厂文件,并在 test/models 目录下创建了我的规范文件。我得到的当前错误是'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 

【问题讨论】:

    标签: ruby-on-rails ruby shoulda factory-bot testunit


    【解决方案1】:

    我的一个项目也遇到过这个问题。我不确定是什么导致初始化代码被跳过,但你可以像这样强制加载工厂定义:

    require 'factory_girl'
    Factory.find_definitions
    

    希望这会有所帮助。

    【讨论】:

    • 这对我有用,方法是把它放在我的 test_helper.rb 文件中。我在使用 factory_girl 1.3.3、rails 2.3.11、ruby 1.8.7 和 bundler 1.0.10 时遇到了这个问题
    • 在做 railstutorial 时为我工作 rails 3.0.9。放在我的 user_helper 文件中。
    • 对于 Rails 3.04、FactoryGirl 3.3.0,使用:FactoryGirl.find_definitions,而不是 Factory.find_definitions
    【解决方案2】:

    尝试将其放入 test_helper.rb:

    require 'factory_girl'
    Dir.glob(File.dirname(__FILE__) + "/factories/*").each do |factory|
      require factory
    end
    

    【讨论】:

      【解决方案3】:

      刚刚发现 factory_girl_rails,它说自动加载是它唯一的额外功能https://github.com/thoughtbot/factory_girl_rails

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题。最终,我将所有工厂放在“/test/factories.rb”中,并在“/test/test_helper.rb”文件中写入以下行:

        require 'factory_girl'
        require File.dirname(__FILE__) + "/factories"
        

        您可以通过在 test_helper 中要求多个文件来对多个文件执行相同的操作。我还没有弄清楚为什么“factory_girl's”自述文件中提到的自动包含没有发生。

        【讨论】:

          【解决方案5】:

          我还设法通过将这一行放入我的 environment.rb 中来解决这个问题:

          config.gem "factory_girl", :source =&gt; "http://gemcutter.org"

          还要确保您拥有最新的 gem:

          名称从“thoughtbot-factory_girl”更改为“factory_girl”,来源从“http://gems.github.com”更改为“http://gemcutter.org”。

          【讨论】:

            【解决方案6】:

            如果您在使用 ruby​​ 1.9.2 时遇到此问题,require 需要扩展路径。

            File.expand_path("test/factories.rb")
            

            patch 解决了我的问题。我刚刚发送了一个拉取请求。 之后,您可以将其添加到您的 test_helper.rb:

            require 'factory_girl'
            FactoryGirl.find_definitions
            

            【讨论】:

              【解决方案7】:

              如果我在 test_helper.rb 中只需要“factory_girl”,我会得到你提到的相同行为,但如果我在我的 config/test/environment.rb 中需要它(注意我使用environmentalist)它会正确找到工厂定义没有任何问题。

              我在阅读了工厂女孩 rdoc 后尝试了这个,它说将 config.gem 放入您的环境中。

              【讨论】:

                【解决方案8】:

                我也遇到了问题 - 在将 FactoryGirl 更新到 1.3.2 之后 - 来自 test/factories 的工厂不再自动加载。

                我可以通过将 dg 中的代码添加到 test_helper.rb 中来解决这个问题:

                Dir.glob(File.dirname(__FILE__) + "/factories/*.rb").each do |factory|
                 require factory
                end
                

                在 Textmate 中运行单个测试时,一切正常,但运行例如从命令行使用 rake test:units 的所有单元测试都因 DuplicateDefinitionError 而失败(我读到它可能与 ruby​​ 1.8.x 有关)。所以我稍微改了一下代码:

                if (!Factory.factories || Factory.factories.empty?)
                  Dir.glob(File.dirname(__FILE__) + "/factories/*.rb").each do |factory|
                    require factory
                  end
                end
                

                【讨论】:

                • 我遇到了完全相同的问题。谢谢!
                【解决方案9】:

                你有没有试过移动

                require 'factory_girl'
                

                到你的 test/test_helper.rb

                工厂自动加载机制可能取决于调用 require 的位置。它可能试图找到工厂 *test/models/factories/** 而不是 *test/factories/**

                【讨论】:

                  【解决方案10】:

                  不要将您的工厂文件命名为 test_factory.rb,而是尝试将其命名为 factory.rb

                  【讨论】:

                  • 文档说 test/factories 目录下的任何 .rb 文件都应该被加载
                  【解决方案11】:

                  有趣。我在尝试让黄瓜与 factory_girl 一起工作时遇到了类似的问题。我最初将 factory_girl 配置为要查找('config.gem'),但未在 cucumber 环境中加载,并且在 'features/support/env.rb' 中完全需要,就像 cucumber 为 webrat 所做的一样,等等。这才开始当我明确告诉 factory_girl 按照上面 Kenny 的建议找到它的定义时工作。

                  当我从 env.rb 中删除 require 语句并在 cucumber 环境中完全需要 factory_girl 时,效果消失了, factory_girl 开箱即用。

                  所以这似乎是一个何时(或在何种情况下)加载 factory_girl 的问题。

                  【讨论】:

                    【解决方案12】:

                    由于不同的人使用不同版本的 Rails(2.x 和 3.x 是现在最常见的),因此包含环境的其他相关部分很重要(最重要的是您使用的是哪个版本的 Rails)重新开始)。来自 factory_girl 网页,版本 1.3.0 文档 (http://rubydoc.info/gems/factory_girl/1.3.0/frames):

                    如果你想在 Rails 3 中使用 factory_girl,请使用 factory_girl_rails gem,不是这个。

                    如果你想在 Rails 3 之前的 Rails 版本中使用 factory_girl, 使用版本 1.2.4。

                    如果您在加载时遇到问题,建议您确保使用正确的版本。我假设,大于 1.2.4 的 factory_girl 版本作为“factory_girl_rails”(Rails 3.0+)gem 的依赖项引入。

                    【讨论】:

                      【解决方案13】:

                      我添加了 require 'factory_girl' require File.dirname(FILE) + "/factories" 到 spec_helper.rb 有帮助,但后来我记得 Spork 有时会有点问题,所以我在没有 require 的情况下重新启动了 Spork,然后它工作正常。

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 2011-02-28
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多