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
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