测试的新手,我正在努力通过一些 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/