【发布时间】:2016-03-29 14:59:05
【问题描述】:
我正在为一个我没有构建的应用程序编写控制器测试,所以这绝对是一个学习过程。这是我第一次遇到直接从 AbstractController::Base 继承的控制器。显然,它的行为与其他控制器不同。
它的格式大致是:
class SchwadGenericController < AbstractController::Base
def schwad_method var_one, var_two = nil, var_three = nil
if var_two.blank?
var_one.generic_method
end
render template: "schwad_templates/generic_template", layout: false
end
end
我尝试了正常的测试,这是我目前的状态,可以让任何事情发生。
require 'rails_helper'
describe SchwadGenericController do
# before(:each) do
# SchwadGenericController.skip_authorize_resource
# end
# login_user
let!(:variable){ create(:my_factory_variable) }
describe 'controller methods' do
it 'should hit this method' do
binding.pry
SchwadGenericController.schwad_method(variable)
# expect(response).to_render template: "schwad_templates/generic_template"
end
end
end
这大概就是我失败的地方。
Failures:
1) SchwadGenericController controller methods should hit this method
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `request=' for # <SchwadGenericController:0x007f8022db0a20>
我在这里阅读了抽象控制器及其在 Rails 中的作用:https://www.mobomo.com/2012/06/and-you-thought-render-farms-were-just-for-pixar/
我在这里阅读了文档:http://api.rubyonrails.org/classes/AbstractController/Base.html
我真的很感激另一组对此的关注以及关于你们如何测试控制器及其方法的指导,这些控制器继承自 AbstractController::Base.... 我错过了什么?
-施瓦德
【问题讨论】:
标签: ruby-on-rails ruby rspec factory-bot