基本上我有下面的测试和课程。我正在尝试测试该方法
new
。但问题是该方法取决于之前的操作set_variables
,这是私有的,所以我不知道如何调用它。我以为当我实例化该类时它会被调用,但看起来并没有。
测试:
require "test_helper"
class MyControllerTest < ActiveSupport::TestCase
test "myTest" do
controller = MyController.new()
response = controller.new()
end
end
控制器类:
class MyController < ApplicationController
before_action :set_variables
def new
@other_variable = @my_variable.stuff.order(:name)
end
private
def set_variables
puts "setting variables"
@my_variable = MyObject.find(params[:my_id])
end
end
我找到了一种方法,在测试中你可以像这样设置实例变量:
controller.instance_variable_set("@my_variable", value)