我的 ApplicationHelper 中有一个方法可以检查我的购物篮中是否有任何元素
module ApplicationHelper
def has_basket_items?
basket = Basket.find(session[:basket_id])
basket ? !basket.basket_items.empty? : false
end
end
这是我必须测试的助手规范:
require 'spec_helper'
describe ApplicationHelper do
describe 'has_basket_items?' do
describe 'with no basket' do
it "should return false" do
helper.has_basket_items?.should be_false
end
end
end
end
但是当我运行测试时我得到了
SystemStackError: stack level too deep
/home/user/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/testing/test_process.rb:13:
通过调试,我看到 session 正在 ActionDispatch::TestProcess 中从 @request.session 访问,@request 为 nil。当我从我的请求规范访问 session 时,@request 是 ActionController::TestRequest 的一个实例。
我的问题是我可以从助手规范访问 session 对象吗?如果可以,怎么做?如果我不能,测试此方法的最佳实践是什么?
****更新****
这要归功于在我的工厂中包含 include ActionDispatch::TestProcess
。删除这个包括排序问题。
最佳答案
我可以从助手规范访问 session 对象吗?
是的。
module ApplicationHelper
def has_basket_items?
raise session.inspect
basket = Basket.find(session[:basket_id])
basket ? !basket.basket_items.empty? : false
end
end
$ rspec spec/helpers/application_helper.rb
Failure/Error: helper.has_basket_items?.should be_false
RuntimeError:
{}
session 对象在那里并返回一个空散列。
尝试更详细地查看回溯以找出错误。 堆栈级别太深
通常表示递归出错。
关于ruby-on-rails - 从 rspec 中的助手规范访问 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13673639/