1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
# frozen_string_literal: true
require "test_helper"
require "support/models"
if defined?(ActionController::API)
class ApiRespondWithController < ActionController::API
respond_to :json
def index
respond_with [
Customer.new("Foo", 1),
Customer.new("Bar", 2),
]
end
def create
respond_with Customer.new("Foo", 1), location: "http://test.host/"
end
end
class RespondWithAPITest < ActionController::TestCase
tests ApiRespondWithController
def test_api_controller_without_view_rendering
@request.accept = "application/json"
get :index
assert_equal 200, @response.status
expected = [{ name: "Foo", id: 1 }, { name: "Bar", id: 2 }]
assert_equal expected.to_json, @response.body
post :create
assert_equal 201, @response.status
expected = { name: "Foo", id: 1 }
assert_equal expected.to_json, @response.body
errors = { name: ["invalid"] }
Customer.any_instance.stubs(:errors).returns(errors)
post :create
assert_equal 422, @response.status
expected = { errors: errors }
assert_equal expected.to_json, @response.body
end
end
end
|