Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.