ruby-on-rails - Rspec Controller 错误,要求<"index">,但使用<""进行渲染>

标签 ruby-on-rails ruby-on-rails-3 rspec devise cancan

测试的新手,我正在努力通过一些 Controller 测试。

以下 Controller 测试将引发错误:

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

我的一项 Controller 规范中包含以下内容:
  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

在我的 Controller 宏中,我有以下内容:
  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-

Controller 代码:
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

关于ruby-on-rails - Rspec Controller 错误,要求<"index">,但使用<""进行渲染>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13075972/

相关文章:

ruby-on-rails - 在 Rails 中渲染大量 ActiveRecord 对象

ruby-on-rails-3 - Heroku_Accounts 和 Bundler 的问题

ruby-on-rails-3 - 厂妹,附属工厂

ruby - 使用 rspec 如何测试以正确顺序接收散列参数的方法?

ruby-on-rails-4 - 使用 Capybara + Poltergeist 等待 AJAX

ruby-on-rails - 从 id 数组更新 has_many 关联

ruby-on-rails - 如何在 Fabrication 的计数参数中使用 transient 属性?

ruby-on-rails - 在 rspec 中编写测试用例时出错

ruby-on-rails - Spree/Ruby on Rails 中的 Assets 路径错误

ruby-on-rails - 如何获取具有 min has_many 记录的记录(加入数据)