在测试失败的视图中使用 CanCanCan

Using CanCanCan in a view failing tests

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

 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 结束

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

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.

我不确定是什么原因造成的,但任何帮助都是很好的

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