Rspec控制器错误期望“<" index>>但是使用<> [英] Rspec controller error expecting <"index"> but rendering with <"">
问题描述
以下控制器测试会抛出错误:
expecting<index>但是使用<
我在我的一个控制器规格中有以下内容:
require'spec_helper'
描述NasController do
render_views
login_user
描述GET #nasdo
它填充一组设备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
它渲染:索引视图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
我正在使用设计和cancan,并遵循他们的指南。测试。我相信我的用户已经登录,并且有能力查看索引操作。
我可以做些什么来让测试通过?
- 更新1 -
控制器代码:
class NasController< ApplicationController
before_filter:authenticate_user!
load_and_authorize_resource
respond_to:js
def index
如果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
我想如果你改变了
:index viewdo
response.should render_template(:index)
end
到
它呈现:索引视图do
get:index
response.should render_template (:index)
end
它应该工作。
更新:尝试这个
它渲染:索引视图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
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
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?
-- UPDATE 1 --
Controller code:
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
I think if you change
it "renders the :index view" do
response.should render_template(:index)
end
to
it "renders the :index view" do
get :index
response.should render_template(:index)
end
it should work.
update: try this
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
这篇关于Rspec控制器错误期望“<" index>>但是使用<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!