Rspec controller error expecting <"index"> but rendering with <"">
Asked Answered
A

2

9

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
Allerus answered 25/10, 2012 at 19:40 Comment(0)
S
14

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
Streaming answered 25/10, 2012 at 20:5 Comment(6)
Hey. That still fails. Tried it earlier - also tried to repeat the location creation. Don't get it. The same test passes in another controller.Allerus
Is their something in your controller that prevents you from bringing up the index if the naz table is empty?Streaming
Nope, and I've tried removing both the devise and cancan filters. And replacing the find with @nas = Nas.all Just in case. Frustrating!Allerus
does your index method in your controller contain a redirect_to anywhere?Streaming
It would help if you could post your controller code for the index action and anything else relevant.Oldie
login_user should go in a before(:each) blockMcmaster
A
1

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"
Awed answered 29/10, 2012 at 0:45 Comment(1)
Thanks for the suggestion. Have tried but this doesn't work for me. Am going to see if I can get it to work in another controller and see differences.Allerus

© 2022 - 2024 — McMap. All rights reserved.