【问题标题】:Rail test case fixtures not loading铁路测试用例夹具未加载
【发布时间】:2011-02-28 10:22:25
【问题描述】:

Rails 似乎不会加载任何用于单元或功能测试的装置。我有一个简单的“products.yml”,可以解析并显示正确:

ruby:
  title: Programming Ruby 1.9
  description:
    Ruby is the fastest growing and most exciting dynamic
    language out there. If you need to get working programs
    delivered fast, you should add Ruby to your toolbox.
  price: 49.50
  image_url: ruby.png

我的控制器功能测试开始于:

require 'test_helper'

class ProductsControllerTest < ActionController::TestCase
  fixtures :products

  setup do
    @product = products(:one)    
    @update = {
      :title => 'Lorem Ipsum' ,
      :description => 'Wibbles are fun!' ,
      :image_url => 'lorem.jpg' ,
      :price => 19.95
    }
  end

根据这本书,Rails 应该“神奇地”加载固定装置(因为我的 test_helper.rb 里面有 fixtures :all。我还添加了显式固定装置加载(见上文)。是的 Rails 抱怨:

user @ host ~/Dropbox/Rails/depot > rake test:functionals
(in /Somewhere/Users/user/Dropbox/Rails/depot)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:test "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/products_controller_test.rb" 
Loaded suite /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
EEEEEEE
Finished in 0.062506 seconds.

  1) Error:
test_should_create_product(ProductsControllerTest):
NoMethodError: undefined method `products' for ProductsControllerTest:Class
    /test/functional/products_controller_test.rb:7

  2) Error:
test_should_destroy_product(ProductsControllerTest):
NoMethodError: undefined method `products' for ProductsControllerTest:Class
    /test/functional/products_controller_test.rb:7
...

我确实遇到了另一个 Rails 测试夹具问题 Rails unit testing doesn't load fixtures,但这会导致插件问题(与加载夹具的顺序有关)。

顺便说一句,我正在使用 Rail 2.3.5 和 Ruby 1.8.7 的 Mac OS X 10.6 上进行开发,没有额外的插件(除了基本安装)。

关于如何调试的任何指示,为什么 Rails 的魔力在这里似乎失败了?是版本问题吗?我可以将代码跟踪到库中并找到答案吗?有这么多“mixin”模块,我找不到 fixtures 方法真正存在的地方。

【问题讨论】:

标签: ruby-on-rails unit-testing fixtures


【解决方案1】:

在 Rails 3.2 中,我发现在运行单个测试文件(指定 TEST=/test/unit/foo.rb)时,未加载固定装置。但是,当使用

运行测试时
ruby -Itest /test/unit/foo.rb

灯具按预期加载。

【讨论】:

    【解决方案2】:

    使用 ruby​​ 1.8.7 和 rails 2.1.1,我通过在 test_helper.rb 中设置 self.use_instantiated_fixtures = true 解决了这个问题。

    【讨论】:

      【解决方案3】:

      放弃后,我尝试在 rail 3.0.0beta3 上重新安装/重建。这解决了这个问题。我只能假设这是某种版本不匹配。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-21
        • 2018-02-07
        • 2011-01-12
        • 2021-07-24
        • 1970-01-01
        • 2011-06-07
        • 2013-10-24
        • 1970-01-01
        相关资源
        最近更新 更多