9

テストは初めてですが、いくつかのコントローラーテストに合格するのに苦労しています。

次のコントローラーテストはエラーをスローします。

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

コントローラーの仕様の1つに次のものがあります。

  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

私のコントローラーマクロには、次のものがあります。

  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

私はdeviseとcancanを使用していて、彼らのガイドに従っています。テスト。ユーザーは以前にサインインしていて、インデックスアクションを表示できると思います。

テストに合格するにはどうすればよいですか?

-更新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
4

2 に答える 2

14

変えたら

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

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

動作するはずです。

更新:これを試してください

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
于 2012-10-25T20:05:36.520 に答える
1

私は自分の側でこのエラーを修正することができましたが、同じ問題が発生しているかどうかはわかりません。

私のセットアップでは、各応答形式(html、js、jsonなど)をループし、その形式で仕様をテストするコントローラーマクロがありました。馬鹿のように、私は実際にいくつかの仕様のjson応答テンプレートが存在していませんでした。また、テンプレートが実際に見つからない場合にエラーがスローされることに気づいていませんでした。それが私の問題でした。

以下のように仕様でフォーマットを指定してみてください。正しいフォルダにindex.htmlという名前のモックテンプレートを書き込んで、同じエラーが発生するかどうかを確認してください。

get :index, :format => "html"
于 2012-10-29T00:45:43.600 に答える