環境:
- ルビー 1.9.3p194
- レール3.2.8
Rails のマウント可能なエンジン 'MyEngine' を作成し、'post' モデルで scaffold を生成すると、次の手順で変更しても機能テストが失敗します。
次の方法でマウント可能なエンジンを作成します。
$ rails plugin new my_engine --mountable $ cd my_engine $ rake db:create; rake db:migrate; rake db:setup $ rails g scaffold post name zip $ rake db:migrate
'rake test' は次のように失敗します:
$ rake test NoMethodError: undefined method `posts' for <MyEngine::PostsControllerTest:0xacd1fa4> ...my_engine/test/functional/my_engine/posts_controller_test.rb:6:in 'block in <class:PostsControllerTest>' ...
https://github.com/rails/rails/issues/4971および http://edgeguides.rubyonrails.org/engines.htmlによると、 「5.1 機能テスト」、次の 2 つの点を変更しました。
次のフラグメントを test/test_helper.rb に追加します。
class ActiveSupport::TestCase fixtures :all end
「テーブル 'my_engine_posts' が見つかりませんでした」というエラーが発生したため、次のようにテスト DB を手動で再作成します。
$ (エクスポート RAILS_ENV=test; rake db:drop; rake db:create; rake db:migrate; rake db:setup)
test/functional/my_engine/posts_controller_test.rb を次のように変更します。
module MyEngine class PostsControllerTest < ActionController::TestCase setup do @post = my_engine_posts(:one) end ^^^^^^^^^^ ... test "should get index" do get :index, use_route: :my_engine ^^^^^^^^^^^^^^^^^^^^^^^
ただし、レーキ テストは次のステップで失敗します。
test_should_create_post(MyEngine::PostsControllerTest): NoMethodError: メソッド `post_path' が未定義です...
'post_path' という名前のルートは、足場で生成された機能テストで使用されます。名前付きルートは問題ないと思います。理由は次のとおりです。
$ rake app:routes my_engine /my_engine MyEngine::Engine Routes for MyEngine::Engine: posts GET /posts(.:format) my_engine/posts#index POST /posts(.:format) my_engine/posts#create new_post GET /posts/new(.:format) my_engine/posts#new edit_post GET /posts/:id/edit(.:format) my_engine/posts#edit post GET /posts/:id(.:format) my_engine/posts#show PUT /posts/:id(.:format) my_engine/posts#update DELETE /posts/:id(.:format) my_engine/posts#destroy
興味深いのは、Rails アプリ (プラグインではない) の足場で生成された同様のステップが、(もちろん!) 次のように機能することです。
$ rails new my_app $ cd my_app/ $ rake db:create; rake db:migrate; rake db:setup $ rails g scaffold post name zip $ rake db:migrate $ rake test
マウント可能なエンジンで足場が生成した機能テストに合格する方法を誰かが助けてくれたら、とても感謝しています!