0
votes

I want to put the common controller actions, index, show, create etc. in the ApplicationController like this:

class ApplicationController < ActionController::Base
  respond_to :json

  def index
    #implementation
  end

  def show
   #implementation
  end

  def update
    #implementation
  end
end

The app will only return JSON.

I have written the following spec to test this with RSPEC's annonymous controller

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

The above spec gives the following error:

ActionView::MissingTemplate: Missing template anonymous/index, application/index with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder]}. Searched in: * "#"

Can anyone suggest a way to make this work with the anonymous controller?

2
Does using xhr :get :index instead of get :index help? - Sam Peacey

2 Answers

0
votes

try this may be it helpful

your controller like

def index
end

your rspec testing like

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

create index.html.erb in your application/ folder

then test it.

0
votes

describe "GET index" do

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

end