在视图中使用CanCanCan失败测试

时间:2016-10-25 15:01:04

标签: ruby ruby-on-rails-3 rspec cancancan

嗨,我有一个能力项目:

 can :set_to_user, Post

然后我有一个观点:

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

在我的帮手中:

def can_set_to_user         能够? :set_to_user,发布    端

当我运行服务器时这很好用,但是当我运行我的测试时,它们似乎崩溃了,给出了这个错误?

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.

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

1 个答案:

答案 0 :(得分: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