0

I am very new to testing with rails and have seemed to got myself lost in the world of testing. I am currently testing my dashboard controller and everything passes when I remove the load_and_authorize_resource line from my controller. I am using cancan for authorization.

dashboard_controller.rb

def update
@dashboard = Dashboard.find(params[:id])

respond_to do |format|
  if @dashboard.update_attributes(params[:dashboard])
    format.html { redirect_to dashboards_path, notice: 'dashboard was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @dashboard.errors, status: :unprocessable_entity }
  end
end
end

dashboard_controller_spec.rb

require 'spec_helper'

describe DashboardsController do

  login_user
  before :each do 
    @dashboard = create(:dashboard, dashboard_name: "My Own Dashboard", id: 1)
  end

  describe "GET #index" do

    it "should have a current_user" do 
      subject.current_user.should_not be_nil
    end

    it "renders the :index view" do
      get :index
      response.should render_template :index
    end

    it "Creates new dashboard" do 
      get :new
      response.should render_template :new
    end

  end

  describe "Get #edit" do 

    it "assigns dashboard to @dashboard" do 
      get :edit, id: @dashboard
      assigns(:dashboard).should == @dashboard
    end

    it "renders the :edit template" do 
      get :edit, id: @dashboard
      response.should render_template :edit
    end

  end


end

The error I receive from console

 1) DashboardsController Get #edit renders the :edit template
 Failure/Error: response.should render_template :edit
   expecting <"edit"> but rendering with <"">
 # ./spec/controllers/dashboard_controller_spec.rb:37:in `block (3 levels) in <top (required)>'

Anyway to bypass this error without removing the load_and_authorize_resource in my dashboard_controller?

coletrain
  • 2,809
  • 35
  • 43
  • Do you have edit and index method in your controller?i am asking because you did not specify those methods in your controller code. – Heena Hussain Nov 28 '12 at 04:57
  • Yes I have an edit method but its the simple find params edit method so I did not include it – coletrain Nov 29 '12 at 01:02

1 Answers1

0

Testing controllers when Devise is involved is a annoyingly trickier. You don't provide your login_user code, but I'm guessing that it's not covering all the bases. As per the official Devise documentation, you need to help things along with warden, Devise's utility methods, etc. I'd repeat here, but it's a bit longer and you mine as well go to the source. The most pertinent part is:

Controller specs won’t work out of the box if you’re using any of devise’s utility methods.

As of rspec-rails-2.0.0 and devise-1.1, the best way to put devise in your specs is simply to add the following into spec_helper...

Then add in their sample controller_macros.rb and tweak some other things and you should be good to go. However, do note that testing your controller specs are different from testing request specs and you might run into hangups in that different scenario that this solution doesn't address (see some of my own pain and discovery at https://stackoverflow.com/a/13275366/9344).

Community
  • 1
  • 1
Ted
  • 7,122
  • 9
  • 50
  • 76