-
-
Save bertomartin/8245531 to your computer and use it in GitHub Desktop.
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
module APIControllerTest | |
def get_json(action, params = {}) | |
get action, params.merge(:format => :json) | |
assert_response :success | |
JSON.parse(@response.body) | |
end | |
def assert_status(expected_code) | |
assert_equal expected_code, @response.status | |
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
require 'test_helper' | |
class ItemsControllerTest < ActionController::TestCase | |
include APIControllerTest | |
def test_it_lists_items | |
get_json(:index) | |
assert_status 200 | |
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
require 'test_helper' | |
class OrdersControllerTest < ActionController::TestCase | |
include APIControllerTest | |
def test_it_shows_orders | |
2.times{ Order.create } | |
data = get_json(:index) | |
assert_equal 2, data.count | |
end | |
def test_it_responds_with_a_single_order | |
order = Order.create | |
data = get_json(:show, :id => order.id) | |
assert_equal order.id, data["id"] | |
end | |
def test_it_creates_an_order | |
params = {:order => {:amount => 9.00, :user_id => 2}, :format => :json} | |
post :create, params | |
assert_status 201 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment