我是Rails的新手。我一直在用RSpec开发我的应用程序。我刚刚使用了Facebooker2的tutorial,并添加了用于身份验证的before_filter。
#app/controller/application_controller.rb
before_filter :ensure_authenticated然而,我的规范失败了,因为没有有效的会话,这是可以理解的。
#spec/controllers/answers_controller_spec.rb
describe "GET index" do
it "assigns all answers as @answers" do
get :index, {}, valid_session
assigns(:answers).should include(answers(:reading))
end
end但是我似乎找不到一种方法来伪造一个有效的会话,以便rspec在下面的函数中使用。
#spec/controllers/answers_controller_spec.rb
def valid_session
{}
end有没有办法呢?如果不是,在这种情况下,rails的处理方式是什么?
发布于 2012-12-08 00:06:27
如果您不关心测试实际的身份验证,而只是想模拟一个有效的会话,那么您可以清除过滤器:
controller.stub(:ensure_authenticated).and_return(true)编辑:您可能需要ApplicationController.stub(...)
https://stackoverflow.com/questions/11081105
复制相似问题