Ruby on rails Rails-使用rspec测试应用程序控制器

Ruby on rails Rails-使用rspec测试应用程序控制器,ruby-on-rails,rspec,Ruby On Rails,Rspec,我想在ApplicationController中放置常见的控制器操作、索引、显示、创建等,如下所示: class ApplicationController < ActionController::Base respond_to :json def index #implementation end def show #implementation end def update #implementation end end 上述

我想在ApplicationController中放置常见的控制器操作、索引、显示、创建等,如下所示:

class ApplicationController < ActionController::Base
  respond_to :json

  def index
    #implementation
  end

  def show
   #implementation
  end

  def update
    #implementation
  end
end
上述规范给出了以下错误:

ActionView::缺少模板:缺少模板匿名/索引, 应用程序/索引带有{:locale=>[:en],:formats=>[:json], :handlers=>[:erb,:builder]}。搜索:* “#”


有人能建议一种方法让匿名控制器工作吗?

试试这可能会有帮助

你的控制器像

def index
end
你的rspec测试像

describe "GET index" do
  it "should respond to index" do
    get :index
    response.code.should eq "200"
  end
end
在应用程序/文件夹中创建index.html.erb

然后测试它

描述“获取索引”的操作


end

是否使用
xhr:get:index
而不是
get:index
help?在索引操作中写入render:nothing=>true,然后进行测试。索引操作将返回一些内容。你的意思是在规范中覆盖它吗?这将不会测试任何东西。
describe "GET index" do
  it "should respond to index" do
    get :index
    response.code.should eq "200"
  end
end
it "returns correct JSON" do
  # @groups.should have(2).items
  get :index, :format => :json
  response.should be_success
  body = JSON.parse(response.body)
  body.should include('group')
  groups = body['group']
  groups.should have(2).items
  groups.all? {|group| group.key?('customers_count')}.should be_true
  groups.any? {|group| group.key?('customer_ids')}.should be_false
end