Testing

Testing a project using JR very similar to testing a traditional Rails application, as described in the Rails Guides.

Models

JR Resources are backed by ActiveRecord models. As such models should be fully tested. This includes validations, scopes, associations, and public methods.

Resources

Resources present a bit of a challenge to test. Many features can not be tested in isolation and require testing in controller or request tests. However the following types of resource features are testable in resource specific tests

Attributes

Computed Attributes Testing
require 'test_helper'

class ContactResourceTest < ActiveSupport::TestCase
def test_full_name
context = {}
contact_resource = ContactResource.new(Contact.new(name_first: "Joe", name_last: "Smith"), context)
assert_equal "Joe Smith", contact_resource.full_name
end
end
Delegated Attributes Testing
class ContactResourceTest < ActiveSupport::TestCase
def test_first_name_delegates
context = {}
contact_resource = ContactResource.new(Contact.new(name_first: "Joe", name_last: "Smith"), context)
assert_equal "Joe", contact_resource.first_name
end
end
Fetchable Attributes Testing

Fetchable fields are computed per resource instance and can use the context to control the inclusion of fields.

class ContactResourceTest < ActiveSupport::TestCase
def test_email_not_visible_to_guest
context = { guest: true }
contact_resource = ContactResource.new(Contact.new(name_first: "Joe", name_last: "Smith", email: "joe@example.com"), context)
refute_includes contact_resource.fetchable_fields, :email
end

def test_email_visible_to_non_guests
context = { guest: false }
contact_resource = ContactResource.new(Contact.new(name_first: "Joe", name_last: "Smith", email: "joe@example.com"), context)
assert_includes contact_resource.fetchable_fields, :email
end
end
Creatable, Updatable, Sortable Attributes Testing

Testing these attribute methods can be done on the resource class.

class ContactResourceTest < ActiveSupport::TestCase
def test_email_not_allowed_for_guests
context = { guest: true }
contact_resource = ContactResource.new(Contact.new(name_first: "Joe", name_last: "Smith", email: "joe@example.com"), context)
refute_includes ContactResource.creatable_fields(context), :email
refute_includes ContactResource.updatable_fields(context), :email
refute_includes ContactResource.sortable_fields(context), :email
end
end

Relationships, Filters, Pagination, and Sorting

These resource concerns interact with the controllers and operation processors and are easier tested with controller or integration tests.

Routes

You can assert routes using the Rails RoutingAssertions. There isn’t a way to refute a route with the built in RoutingAssertions, however this can be

Routes can be refuted using an integration test.

Controllers

The entire request can be tested with controller tests. For example:

class ContactsControllerTest < ActionController::TestCase
# Put this in a test helper
def json_response
JSON.parse(@response.body)
end

def test_get_all_contacts
get :index
assert_response :success
assert_equal Contact.count, json_response['data'].size
end
end