期望<"index>的Rspec控制器错误但是用<">渲染 [英] Rspec controller error expecting <"index"> but rendering with <"">

查看:69
本文介绍了期望<"index>的Rspec控制器错误但是用<">渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始测试,我正在努力让一些控制器测试通过.

New to testing, I'm struggling to get some controller tests to pass.

以下控制器测试抛出错误:

The following controller test throws the error:

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

我的控制器规格之一包含以下内容:

I have the following in one of my controller specs:

  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

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

In my controller macros, I have this:

  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,并遵循了他们的指南.测试.我相信我的用户之前已登录并且能够查看索引操作.

I'm using devise and cancan and have followed their guides re. testing. I believe my users are signed in before and have the ability to view the index action.

我该怎么做才能通过测试?

What can I do to get the tests to pass?

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

这篇关于期望&amp;lt;&amp;quot;index&amp;gt;的Rspec控制器错误但是用&lt;&amp;quot;&amp;gt;渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆