Skip to content

Instantly share code, notes, and snippets.

@groyoh
Last active November 20, 2016 15:32
Show Gist options
  • Save groyoh/68b28837fe0774d791727781ffe9d1ed to your computer and use it in GitHub Desktop.
Save groyoh/68b28837fe0774d791727781ffe9d1ed to your computer and use it in GitHub Desktop.
AMS #1971
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '4.2.6'
gem 'active_model_serializers', "~>0.10.2"
end
require 'action_controller/railtie'
class TestApp < Rails::Application
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: 'cookie_store_key'
secrets.secret_token = 'secret_token'
secrets.secret_key_base = 'secret_key_base'
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.logger = Logger.new($stdout)
Rails.logger = config.logger
end
TestApp.initialize!
TestApp.routes.draw do
get 'foos/:id', to: 'test#foo', as: 'foo'
get 'bars/:id', to: 'test#bar', as: 'bar'
end
TestApp.routes.default_url_options = {
host: 'michael-reeves.example.com'
}
class Foo < ActiveModelSerializers::Model
attr_accessor :id, :name
end
class Bar < ActiveModelSerializers::Model
attr_accessor :id, :name
end
class FooSerializer < ActiveModel::Serializer
attributes :id, :name
link(:self) do
foo_url(object.id)
end
end
class BarSerializer < ActiveModel::Serializer
include ActiveModelSerializers::SerializationContext::UrlHelpers
attributes :id, :name
attribute :links do
{ self: bar_url(object.id) }
end
end
require 'rack/test'
class TestController < ActionController::Base
include Rails.application.routes.url_helpers
include ::ActionController::Serialization
def foo
foo = Foo.new(id: 1, name: "parent")
render json: foo, adapter: :json_api
end
def bar
bar = Bar.new(id: 1, name: "parent")
render json: bar, adapter: :json
end
end
require 'minitest/autorun'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
class BugTest < Minitest::Test
include Rack::Test::Methods
def test_foo_link
get '/foos/1'
expected_result = {
data: {
id: "1",
type: "foos",
attributes: {
name: "parent"
},
links: {
self: "http://michael-reeves.example.com/foos/1"
}
}
}
assert_equal(expected_result, JSON.parse(last_response.body, symbolize_names: true))
end
def test_bar_link
get '/bars/1'
expected_result = {
bar: {
id: 1,
name: "parent",
links: {
self: "http://michael-reeves.example.com/bars/1"
}
}
}
assert_equal(expected_result, JSON.parse(last_response.body, symbolize_names: true))
end
private
def app
Rails.application
end
end
__END__
Resolving dependencies...
Using rake 11.3.0
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.9.1
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.1.0
Using rack 1.6.5 (was 1.6.4)
Using mime-types-data 3.2016.0521
Using jsonapi-parser 0.1.1.beta3
Using jsonapi-renderer 0.1.1.beta1
Using thor 0.19.1
Using arel 6.0.3
Using bundler 1.13.6
Using concurrent-ruby 1.0.2
Using tzinfo 1.2.2
Using nokogiri 1.6.8.1
Using rack-test 0.6.3
Using mime-types 3.1
Using jsonapi 0.1.1.beta6 (was 0.1.1.beta2)
Using sprockets 3.7.0
Using activesupport 4.2.6 (was 4.2.7.1)
Using loofah 2.0.3
Using mail 2.6.4
Using rails-deprecated_sanitizer 1.0.3
Using globalid 0.3.7
Using activemodel 4.2.6 (was 4.2.7.1)
Using rails-html-sanitizer 1.0.3
Using rails-dom-testing 1.0.7
Using activejob 4.2.6
Using activerecord 4.2.6 (was 4.2.7.1)
Using actionview 4.2.6 (was 4.2.7.1)
Using actionpack 4.2.6 (was 4.2.7.1)
Using actionmailer 4.2.6
Using railties 4.2.6 (was 4.2.7.1)
Using sprockets-rails 3.2.0
Using active_model_serializers 0.10.2
Using rails 4.2.6
config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:
* development - set it to false
* test - set it to false (unless you use a tool that preloads your test environment)
* production - set it to true
Run options: --seed 53169
# Running:
D, [2016-11-20T16:31:21.805458 #8387] DEBUG -- :
D, [2016-11-20T16:31:21.805505 #8387] DEBUG -- :
I, [2016-11-20T16:31:21.805805 #8387] INFO -- : Started GET "/foos/1" for 127.0.0.1 at 2016-11-20 16:31:21 +0100
I, [2016-11-20T16:31:21.807897 #8387] INFO -- : Processing by TestController#foo as HTML
I, [2016-11-20T16:31:21.807942 #8387] INFO -- : Parameters: {"id"=>"1"}
I, [2016-11-20T16:31:21.816001 #8387] INFO -- : Rendered FooSerializer with ActiveModelSerializers::Adapter::JsonApi (2.27ms)
I, [2016-11-20T16:31:21.816399 #8387] INFO -- : Completed 200 OK in 8ms (Views: 6.9ms)
.D, [2016-11-20T16:31:21.817277 #8387] DEBUG -- :
D, [2016-11-20T16:31:21.817302 #8387] DEBUG -- :
I, [2016-11-20T16:31:21.817380 #8387] INFO -- : Started GET "/bars/1" for 127.0.0.1 at 2016-11-20 16:31:21 +0100
I, [2016-11-20T16:31:21.817991 #8387] INFO -- : Processing by TestController#bar as HTML
I, [2016-11-20T16:31:21.818033 #8387] INFO -- : Parameters: {"id"=>"1"}
I, [2016-11-20T16:31:21.819839 #8387] INFO -- : Rendered BarSerializer with ActiveModelSerializers::Adapter::Json (0.94ms)
I, [2016-11-20T16:31:21.820153 #8387] INFO -- : Completed 200 OK in 2ms (Views: 1.8ms)
.
Finished in 0.023684s, 84.4464 runs/s, 84.4464 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment