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

新的测试,我很难让一些控制器测试通过。

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

expecting  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 

我想如果你改变了

 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 

我设法修复了这个错误,但不知道我们是否遇到了同样的问题。

在我的设置中,我有一个控制器宏,它将遍历每个响应格式(html,js,json等)并测试该格式下的规格。 就像一个白痴,我实际上没有一个json响应模板存在我的一些规格,我没有意识到,如果它实际上找不到模板,这将引发错误。 这就是我的问题。

尝试在下面的规范中指定一种格式,然后在正确的文件夹中写一些名为index.html的模拟模板,看看是否收到同样的错误。

 get :index, :format => "html"