环境:
- 红宝石 1.9.3p194
- 导轨 3.2.8
当我创建 rails 的可安装引擎“MyEngine”并在“post”模型上生成脚手架时,即使我通过以下步骤进行了修改,功能测试也会失败:
通过以下方式创建可安装引擎:
$ 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功能测试”,我修改了两处:
将以下标志附加到 test/test_helper.rb:
class ActiveSupport::TestCase fixtures :all end
由于发生“找不到表'my_engine_posts'”错误,手动重新创建测试数据库如下:
$ (export 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 ^^^^^^^^^^^^^^^^^^^^^^^
但是,在以下步骤中仍然 rake 测试失败:
test_should_create_post(MyEngine::PostsControllerTest): NoMethodError: undefined method `post_path' for...
其中“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 app(不是插件)脚手架生成功能测试的类似步骤(当然!)如下:
$ 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
如果有人帮助我如何在可安装引擎上通过脚手架生成的功能测试,我将非常感激!