リファインメント機能を使用してコントローラアクションをスタブすることはできますか?Ruby洗練を使ってテストでのコントローラアクションの動作を変更することは可能ですか?
私は "my_controller_refinement.rb"
require "my_controller"
module MyControllerRefinement
refine MyController do
def create_item(my_object = {})
return "test_id"
end
end
end
そして、次のようにテストでそれを使用して洗練を定義しています -
test/
--> my_controller_refinement.rb
--> my_controller_test.rb
- テストdirがようである
require_relative "my_controller_refinement"
class MyControllerTest < ActionController::TestCase
using MyControllerRefinement
test "get item" do
post :create { my_object: { name: "Test", id: "test_id" } }
# Post redirects to the show page
assert_redirected_to action: "show", id: "test_id"
end
end
しかし洗練されていないと実際のコントローラのアクションが呼び出されるようです。
このような「スタブ」には何かが欠けていたり、洗練されていないのですか?
代わりに[模擬ライブラリ](https://www.ruby-toolbox.com/categories/mocking)を使用するほうがよいでしょう。さらに、[ActionController :: TestCaseはRails 5で償却されます](http://blog.bigbinary.com/2016/04/19/changes-to-test-controllers-in-rails-5.html)ので、コントローラーテストでの統合のプログラムを考え直してください。 – max