是否可以使用优化功能来 stub Controller 操作?
我在“my_controller_refinement.rb”中定义细化
require "my_controller"
module MyControllerRefinement
refine MyController do
def create_item(my_object = {})
return "test_id"
end
end
end
并在测试中使用它,如下 -
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
测试目录为 -
test/
--> my_controller_refinement.rb
--> my_controller_test.rb
但是细化并没有开始,实际的 Controller 操作似乎被调用了。
我是否遗漏了一些东西,或者改进不能用于这种“ stub ”?
最佳答案
这行不通,因为 Refinements目前工作。文档(如下引用)提供了完整的独家新闻,但实质上细化的范围非常狭窄。
You may only activate refinements at top-level, not inside any class, module or method scope. You may activate refinements in a string passed to Kernel#eval that is evaluated at top-level. Refinements are active until the end of the file or the end of the eval string, respectively.
Refinements are lexical in scope. When control is transferred outside the scope the refinement is deactivated. This means that if you require or load a file or call a method that is defined outside the current scope the refinement will be deactivated.
关于ruby-on-rails - 是否可以使用 ruby 改进来更改测试中 Controller 操作的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44052660/