在视图中使用CanCanCan失败测试

问题描述 投票:1回答:1

嗨,我有一个能力的项目:

 can :set_to_user, Post

然后,我有一个观点:

<div class: "btn btn-primary #{disable_button(!(can_set_to_user))}>

我有一个帮手:

def can_set_to_user可以吗? :set_to_user,post end

这在我运行服务器时工作正常,但是当我运行我的测试时,他们似乎崩溃了,给出了这个错误?

ActionView::Template::Error:
       Devise could not find the `Warden::Proxy` instance on your request environment.
       Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
       If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you.

我不确定是什么导致这种情况,但任何帮助都会很棒

ruby ruby-on-rails-3 rspec cancancan
1个回答
3
投票

Warden::Proxy用于current_useruser_signed_in?方法。您必须在视图规范中存根这些方法才能使其工作:

let(:user) { FactoryGirl.create :user }

before(:example) do
  allow(view).to receive(:current_user).and_return(user)
  allow(view).to receive(:user_signed_in?).and_return(!!user)
end

devise source

© www.soinside.com 2019 - 2024. All rights reserved.