Skip to content

Instantly share code, notes, and snippets.

@jdudek
Created September 13, 2011 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdudek/1212860 to your computer and use it in GitHub Desktop.
Save jdudek/1212860 to your computer and use it in GitHub Desktop.
Testing REST API using BBQ
require File.dirname(__FILE__) + "/../test_helper"
class GameApiTest < Bbq::TestCase
background do
@admin = TestUser.new
@admin.roles(:admin)
@admin.prepare_game_data
@client = TestClient.new
@client.extend(TestClient::GameClient)
end
scenario "client chooses a region" do
@client.login_to_game
@client.get "/challenge/player"
assert_nil @client.received_json["player"]["region"]["id"]
@client.put "/challenge/player", { :player => { :region_id => Region.first.id } }
assert @client.received_response_status?(200)
@client.get "/challenge/player"
assert_equal Region.first.id, @client.received_json["player"]["region"]["id"]
end
end
class TestClient
attr_accessor :rack_test
def initialize
self.rack_test = ::TestClient::RackTest.new
end
delegate :get, :post, :put, :to => :rack_test
def received_response_status?(status)
rack_test.last_response.status == status
end
def received_json
JSON.parse(rack_test.last_response.body)
end
class RackTest
include Rack::Test::Methods
def app
Rails.application
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment