我正在尝试测试模型方法引发错误时触发的 Rails 控制器分支。
def my_controller_method
@my_object = MyObject.find(params[:id])
begin
result = @my_object.my_model_method(params)
rescue Exceptions::CustomError => e
flash.now[:error] = e.message
redirect_to my_object_path(@my_object) and return
end
# ... rest irrelevant
end
如何获得 Minitest 存根来引发此错误?
it 'should show redirect on custom error' do
my_object = FactoryGirl.create(:my_object)
# stub my_model_method to raise Exceptions::CustomError here
post :my_controller_method, :id => my_object.to_param
assert_response :redirect
assert_redirected_to my_object_path(my_object)
flash[:error].wont_be_nil
end