Jos*_*eim 4 rspec ruby-on-rails request
我有这个方法ApplicationHelper:
def home_link_class
classes = ['navbar-brand']
classes << 'active' if request.path == root_path
classes
end
Run Code Online (Sandbox Code Playgroud)
我想像这样测试它:
describe '#home_link_class' do
before { allow(helper.request).to receive(:path).and_return '/some-path' }
subject { home_link_class }
it { should eq ['navbar-brand'] }
end
Run Code Online (Sandbox Code Playgroud)
遗憾的是,存根似乎不起作用,request帮助器本身的对象被设置为nil,即使在规范中它似乎是一个ActionController::TestRequest对象.
如何确保request规格中提供?
inf*_*sed 15
您需要存根请求本身以及路径的返回值.为存根请求定义测试双精度path:
describe '#home_link_class' do
let(:request) { double('request', path: '/some-path') }
before { allow(helper).to receive(:request).and_return(request) }
subject { home_link_class }
it { should eq ['navbar-brand'] }
end
Run Code Online (Sandbox Code Playgroud)