2012-10-25 50 views
9

新的測試,我努力獲得一些控制器測試通過。Rspec控制器錯誤預計<"index">但呈現與<"">

以下控制器測試引發錯誤:

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

我在控制器規格下列操作之一:

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 

我正在使用設計和康康,並遵循他們的指導重新。測試。我相信我的用戶之前登錄過,並且有能力查看索引操作。

我該怎麼做才能讓測試通過?

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

回答

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

嘿。那仍然失敗。之前嘗試過 - 也試圖重複創建地點。不明白。另一個控制器通過相同的測試。 – simonmorley

+0

他們的東西在你的控制器,防止你提出索引如果納茨表是空的? –

+0

不,我試過去除了設計和cancan濾鏡。並用@nas = Nas.all替換查找以防萬一。令人沮喪! – simonmorley

1

我設法解決我到底這錯誤,但不知道如果我們有同樣的問題。

在我的設置中,我有一個控制器宏,可以遍歷每個響應格式(html,js,json等),並在該格式下測試規範。就像一個白癡,我實際上並沒有一個json響應模板存在於我的某些規範中,而且我沒有意識到,如果它實際上找不到模板,將會拋出一個錯誤。所以這是我的問題。

嘗試在下面的規範中指定一個格式,然後在正確的文件夾中寫下一個名爲index.html的模擬模板,看看是否得到相同的錯誤。

get :index, :format => "html" 
+0

感謝您的建議。已經嘗試過,但這不適合我。我會看看我是否可以讓它在另一個控制器中工作,並看到不同之處。 – simonmorley

+0

至少在我的情況下,這正是問題,除了我需要「js」。 – Karen

相關問題