0

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

class ApplicationController < ActionController::Base
  respond_to :json

  def index
    #implementation
  end

  def show
   #implementation
  end

  def update
    #implementation
  end
end

该应用程序将仅返回 JSON。

我已经编写了以下规范以使用 RSPEC 的匿名控制器进行测试

describe ApplicationController do
  controller do ; end

  describe 'Get :index' do
    it 'should respond to index' do
      get :index

      response.code.should eq "200"
    end
  end
end

上面的规范给出了以下错误:

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

任何人都可以建议一种使用匿名控制器进行这项工作的方法吗?

4

2 回答 2

0

描述“获取索引”做

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

结尾

于 2012-10-05T07:12:39.053 回答
0

试试这个可能会有帮助

你的控制器喜欢

def index
end

你的 rspec 测试像

describe "GET index" do
  it "should respond to index" do
    get :index
    response.code.should eq "200"
  end
end

在您的 application/ 文件夹中创建 index.html.erb

然后测试它。

于 2012-10-05T04:43:24.693 回答