12

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 does not load Fixturesに出くわしましたが、それはプラグインの問題につながります(フィクスチャをロードする順序と関係があります)。

ところで、私は Rail 2.3.5 と Ruby 1.8.7 を使用して Mac OS X 10.6 で開発しています。追加のプラグインはありません (基本インストール以外)。

Railsの魔法がここで失敗しているように見えるのはなぜですか? バージョンの問題ですか?ライブラリのコードをたどって答えを見つけることはできますか? fixturesメソッドが実際に存在する場所を見つけることができないほど多くの「ミックスイン」モジュールがあります。

4

3 に答える 3

2

Rails 3.2 では、単一のテスト ファイル (TEST=/test/unit/foo.rb を指定) を実行すると、フィクスチャが読み込まれないことがわかりました。ただし、テストを実行する場合

ruby -Itest /test/unit/foo.rb

フィクスチャは期待どおりにロードされます。

于 2012-09-01T06:23:19.253 に答える
1

ruby 1.8.7 と rails 2.1.1 を使用してself.use_instantiated_fixtures = true、test_helper.rb に設定することで問題を解決しました。

于 2010-10-04T14:30:23.967 に答える
0

あきらめて、レール3.0.0beta3に再インストール/再構築してみました。これで問題は解決しました。これはある種のバージョンの不一致だったとしか思えません。

于 2010-06-07T00:57:52.020 に答える