Rspec控制器错误需要<"index">但是使用<"">进行渲染

sim*_*ley 9 rspec ruby-on-rails devise cancan ruby-on-rails-3

新的测试,我正在努力让一些控制器测试通过.

以下控制器测试将引发错误:

   expecting <"index"> but rendering with <"">
Run Code Online (Sandbox Code Playgroud)

我在我的一个控制器规范中有以下内容:

  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
Run Code Online (Sandbox Code Playgroud)

在我的控制器宏中,我有这个:

  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
Run Code Online (Sandbox Code Playgroud)

我正在使用装饰和康康,并且已经跟随他们的导游了.测试.我相信我的用户之前已登录,并且能够查看索引操作.

我能做些什么来让测试通过?

- 更新1 -

控制器代码:

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
Run Code Online (Sandbox Code Playgroud)

Ian*_*mit 14

我想如果你改变了

it "renders the :index view" do
  response.should render_template(:index)
end
Run Code Online (Sandbox Code Playgroud)

it "renders the :index view" do
  get :index
  response.should render_template(:index)
end
Run Code Online (Sandbox Code Playgroud)

它应该工作.

更新:试试这个

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
Run Code Online (Sandbox Code Playgroud)