require_relative '../../test_helper' class API<%= api_version %><%= res_name.classify.pluralize %>Test < TestCase include Rack::Test::Methods def app API<%= api_version %>::Base end def setup super @<%= res_name %>1 = <%= res_name.pluralize %>(:one) @<%= res_name %>2 = <%= res_name.pluralize %>(:two) @all_<%= res_name.pluralize %> = <%= res_name.classify %>.all @not_found_response = { error: '<%= res_name.classify %> not found' } end def teardown super end def test_get_all_<%= res_name.pluralize %> get '/<%= app_prefix %>/<%= api_version %>/<%= res_name.pluralize %>' assert_equal 200, last_response.status assert_equal @all_<%= res_name.pluralize %>.to_json, last_response.body end def test_get_<%= res_name %> get "/<%= app_prefix %>/<%= api_version %>/<%= res_name.pluralize %>/#{@<%= res_name %>1.id}" assert_equal 200, last_response.status assert_equal @<%= res_name %>1.to_json, last_response.body end def test_get_<%= res_name %>_by_id_not_found get "/<%= app_prefix %>/<%= api_version %>/<%= res_name.pluralize %>/1000" assert_equal 404, last_response.status assert_equal @not_found_response.to_json, last_response.body end def test_create_<%= res_name %> body = { <% res_attrs.each do |name, type| -%> <%= name %>: <%= sample_value(type) %>, <% end -%> } <%= res_name %>_count = <%= res_name.classify %>.count post '/<%= app_prefix %>/<%= api_version %>/<%= res_name.pluralize %>', body, { 'Content-Type' => 'application/json' } assert_equal 201, last_response.status assert_equal <%= res_name %>_count + 1, <%= res_name.classify %>.count end def test_update_<%= res_name %> body = { <% res_attrs.each do |name, type| -%> <%= name %>: <%= sample_value(type) %>, <% end -%> } put "/<%= app_prefix %>/<%= api_version %>/<%= res_name.pluralize %>/#{@<%= res_name %>1.id}", body, { 'Content-Type' => 'application/json' } assert_equal 204, last_response.status <% res_attrs.each do |name, type| -%> assert_equal body[:<%= name %>], <%= res_name.classify %>.find(@<%= res_name %>1.id).<%= name %> <% end -%> end def test_delete_<%= res_name %> <%= res_name %>_count = <%= res_name.classify %>.count delete "/<%= app_prefix %>/<%= api_version %>/<%= res_name.pluralize %>/#{@<%= res_name %>1.id}" assert_equal 204, last_response.status assert_equal <%= res_name %>_count - 1, <%= res_name.classify %>.count end end