ryanb/cancan — Issue #488: Problems using rspec with cancan and devise (expecting <"edit"> but rendering with <"">)
Author: jan0sch
Created: 2011-10-10 11:25:33+00:00
State: open

Hi,

I'm using devise and cancan for my current app and I'm running into issues with rspec.
Regarding to the wiki I tried both methods described [in the wiki](https://github.com/ryanb/cancan/wiki/Testing-Abilities) but they only seem to work for the index and show method tests in my controllers.

I use a user model that can hold several roles.

Code from ability.rb:
```
user ||= User.new
if user.is?(:admin)
can :manage, :all
else
can :read, :all
cannot :read, User, :id != user.id
end
```

Code from my controller specs:
```
def setup
@current_user = FactoryGirl.create(:admin)
sign_in @current_user
end
```

Now the error messages for one of my controllers, as mentioned :index and :show work fine.

Failures:

1) CompaniesController new action should render new template
Failure/Error: response.should render_template(:new)
expecting <"new"> but rendering with <"">
# ./spec/controllers/companies_controller_spec.rb:26:in `block (2 levels) in '

2) CompaniesController create action should render new template when model is invalid
Failure/Error: response.should render_template(:new)
expecting <"new"> but rendering with <"">
# ./spec/controllers/companies_controller_spec.rb:32:in `block (2 levels) in '

3) CompaniesController create action should redirect when model is valid
Failure/Error: response.should redirect_to(company_url(assigns[:company]))
ActionController::RoutingError:
No route matches {:action=>"show", :controller=>"companies", :id=>#}
# ./spec/controllers/companies_controller_spec.rb:42:in `block (2 levels) in '

4) CompaniesController edit action should render edit template
Failure/Error: response.should render_template(:edit)
expecting <"edit"> but rendering with <"">
# ./spec/controllers/companies_controller_spec.rb:48:in `block (2 levels) in '

5) CompaniesController update action should render edit template when model is invalid
Failure/Error: response.should render_template(:edit)
expecting <"edit"> but rendering with <"">
# ./spec/controllers/companies_controller_spec.rb:54:in `block (2 levels) in '

6) CompaniesController update action should redirect when model is valid
Failure/Error: response.should redirect_to(company_url(assigns[:company]))
Expected response to be a redirect to but was a redirect to .
# ./spec/controllers/companies_controller_spec.rb:60:in `block (2 levels) in '

7) CompaniesController destroy action should destroy model and redirect to index action
Failure/Error: response.should redirect_to(companies_url)
Expected response to be a redirect to but was a redirect to .
# ./spec/controllers/companies_controller_spec.rb:66:in `block (2 levels) in '

Using ```session[:user_id] = @current_user.id``` instead of ```sign_in @current_user``` has no effect.