Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec controller error expecting <"index"> but rendering with <"">

New to testing, I'm struggling to get some controller tests to pass.

The following controller test throws the error:

   expecting <"index"> but rendering with <"">

I have the following in one of my controller specs:

  require 'spec_helper'

  describe NasController do

  render_views
  login_user

  describe "GET #nas" do
      it "populates an array of devices" do
        @location = FactoryGirl.create(:location)
        @location.users << @current_user
        @nas = FactoryGirl.create(:nas, location_id: @location.id )      
        get :index
        assigns(:nas).should eq([@nas])
      end

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

In my controller macros, I have this:

  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @current_user = FactoryGirl.create(:user)
      @current_user.roles << Role.first
      sign_in @current_user
      User.current_user = @current_user
      @user = @current_user
      @ability = Ability.new(@current_user)
    end
  end

I'm using devise and cancan and have followed their guides re. testing. I believe my users are signed in before and have the ability to view the index action.

What can I do to get the tests to pass?

-- UPDATE 1 --

Controller code:

class NasController < ApplicationController
   before_filter :authenticate_user!
   load_and_authorize_resource

   respond_to :js

   def index

     if params[:location_id]
       ...
     else
     @nas = Nas.accessible_by(current_ability).page(params[:page]).order(sort_column + ' ' + sort_direction)

     respond_to do |format|
      format.html # show.html.erb
     end    
    end
  end
like image 388
simonmorley Avatar asked Oct 25 '12 19:10

simonmorley


2 Answers

I think if you change

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

to

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

it should work.

update: try this

it "renders the :index view" do
  @location = FactoryGirl.create(:location)
  @location.users << @current_user
  @nas = FactoryGirl.create(:nas, location_id: @location.id ) 
  get :index
  response.should render_template(:index)
end
like image 74
Ian Armit Avatar answered Oct 20 '22 08:10

Ian Armit


I managed to fix this error on my end, but no idea if we're having the same problem.

In my setup, I had a controller macro that would loop through each response format (html, js, json, etc.) and test the specs under that format. Like an idiot, I didn't actually have a json response template in existance for some of my specs yet, and I didn't realize that this will throw an error if it can't actually find the template. So that was my issue.

Try specifying a format in your in your specs like below, and just write up some mock template named index.html in the correct folder and see if you get the same error.

get :index, :format => "html"
like image 44
Bryce Avatar answered Oct 20 '22 07:10

Bryce