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
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With