测试新手,我很难通过一些控制器测试。
以下控制器测试会引发错误:
expecting <"index"> 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
答案 0 :(得分:14)
我想如果你改变了
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
答案 1 :(得分:1)
我设法修复了这个错误,但不知道我们是否遇到了同样的问题。
在我的设置中,我有一个控制器宏,它将遍历每个响应格式(html,js,json等),并测试该格式下的规格。就像一个白痴,我实际上没有一个json响应模板存在我的一些规格,我没有意识到,如果它实际上找不到模板,这将引发错误。这就是我的问题。
尝试在下面的规范中指定一种格式,然后在正确的文件夹中写下一些名为index.html的模拟模板,看看是否收到同样的错误。
get :index, :format => "html"