user2320239 user2320239 - 2 months ago 57
Ruby Question

Using CanCanCan in a view failing tests

Hi I have a project with an ability:

can :set_to_user, Post

I then have in one of my views:

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

in a helper I have:

def can_set_to_user
can? :set_to_user, post

This works fine when I run the server, but when I run my tests they seem to crash giving this 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.

I'm not sure what is causing this, but any help would be great

ole ole

Warden::Proxy is being used for the current_user and user_signed_in? methods. You have to stub this methods in your view specs to make it works:

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)

See the devise source