Skip to content

Instantly share code, notes, and snippets.

@darkofabijan
Created June 27, 2011 12:39
Show Gist options
  • Save darkofabijan/1048780 to your computer and use it in GitHub Desktop.
Save darkofabijan/1048780 to your computer and use it in GitHub Desktop.
class Api::V2::MunicipalitiesController < Api::V2::BaseController
def index
municipalities = Municipality.find_all_for_api
@municipalities = municipalities.collect { |m| {:id => m.id, :name => m.name} }
render_json(@municipalities)
end
end
require 'spec_helper'
describe Api::V2::MunicipalitiesController do
let(:api_key) { mock_model(ApiKey, :key => "123") }
let(:municipality) { mock_model(Municipality, :name => "Munun.. 1") }
describe "GET 'index'" do
context "no municipalities" do
it "returns empty json" do
Municipality.should_receive(:find_all_for_api).and_return([])
get :index, :format => 'json', :api_key => api_key.key
response.body.should == '[]'
end
end
context "two municipalities" do
it "returns json with names" do
Municipality.should_receive(:find_all_for_api).and_return([municipality])
get :index, :format => 'json', :api_key => api_key.key
expected_response = %([{"name":"#{municipality.name}","id":#{municipality.id}}]')
response.body.should == expected_response
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment