铁路测试箱固定装置未装载

时间:2010-06-06 17:26:15

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

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抱怨:< / p>

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方法真正存在的地方。

3 个答案:

答案 0 :(得分:2)

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

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

按预期加载灯具。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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