Created
September 13, 2011 00:26
-
-
Save jdudek/1212860 to your computer and use it in GitHub Desktop.
Testing REST API using BBQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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