mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Cleaned up tests
This commit is contained in:
parent
d025cdba34
commit
c599c489c3
1 changed files with 34 additions and 45 deletions
|
|
@ -1,49 +1,51 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
require 'spec_fakes'
|
require 'spec_fakes'
|
||||||
require "grape-active_model_serializers"
|
require "grape-active_model_serializers"
|
||||||
# require 'active_model'
|
|
||||||
|
|
||||||
describe Grape::ActiveModelSerializers do
|
describe Grape::ActiveModelSerializers do
|
||||||
subject do
|
let(:app) { Class.new(Grape::API) }
|
||||||
Class.new(Grape::API)
|
|
||||||
end
|
|
||||||
|
|
||||||
before do
|
before do
|
||||||
subject.format :json
|
app.format :json
|
||||||
subject.formatter :json, Grape::Formatter::ActiveModelSerializers
|
app.formatter :json, Grape::Formatter::ActiveModelSerializers
|
||||||
end
|
end
|
||||||
|
|
||||||
def app
|
|
||||||
subject
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should use the built in grape serializer when serializer is set to nil" do
|
|
||||||
subject.get("/home", serializer: nil) do
|
|
||||||
{user: {first_name: "JR", last_name: "HE"}}
|
|
||||||
end
|
|
||||||
get("/home")
|
|
||||||
last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should respond with proper content-type" do
|
it "should respond with proper content-type" do
|
||||||
subject.get("/home/users", :serializer => UserSerializer) do
|
app.get("/home/users", :serializer => UserSerializer) do
|
||||||
User.new
|
User.new
|
||||||
end
|
end
|
||||||
get("/home/users")
|
get("/home/users")
|
||||||
last_response.headers["Content-Type"].should == "application/json"
|
last_response.headers["Content-Type"].should == "application/json"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should infer serializer when there is no serializer set" do
|
context 'serializer is set to nil' do
|
||||||
subject.get("/home") do
|
before do
|
||||||
User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'})
|
app.get("/home", serializer: nil) do
|
||||||
|
{user: {first_name: "JR", last_name: "HE"}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
it 'uses the built in grape serializer' do
|
||||||
|
get("/home")
|
||||||
|
last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "serializer isn't set" do
|
||||||
|
before do
|
||||||
|
app.get("/home") do
|
||||||
|
User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'infers the serializer' do
|
||||||
get "/home"
|
get "/home"
|
||||||
last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
|
last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "should serializer arrays of objects" do
|
it "serializes arrays of objects" do
|
||||||
subject.get("/home") do
|
app.get("/home") do
|
||||||
user = User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'})
|
user = User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'})
|
||||||
[user, user]
|
[user, user]
|
||||||
end
|
end
|
||||||
|
|
@ -52,9 +54,9 @@ describe Grape::ActiveModelSerializers do
|
||||||
last_response.body.should == "{\"users\":[{\"first_name\":\"JR\",\"last_name\":\"HE\"},{\"first_name\":\"JR\",\"last_name\":\"HE\"}]}"
|
last_response.body.should == "{\"users\":[{\"first_name\":\"JR\",\"last_name\":\"HE\"},{\"first_name\":\"JR\",\"last_name\":\"HE\"}]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
context "for models with compound names" do
|
context "models with compound names" do
|
||||||
it "should generate the proper 'root' node for individual objects" do
|
it "generates the proper 'root' node for individual objects" do
|
||||||
subject.get("/home") do
|
app.get("/home") do
|
||||||
BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'})
|
BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -62,8 +64,8 @@ describe Grape::ActiveModelSerializers do
|
||||||
last_response.body.should == "{\"blog_post\":{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"}}"
|
last_response.body.should == "{\"blog_post\":{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"}}"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should generate the proper 'root' node for serialized arrays of objects" do
|
it "generates the proper 'root' node for serialized arrays" do
|
||||||
subject.get("/home") do
|
app.get("/home") do
|
||||||
blog_post = BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'})
|
blog_post = BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'})
|
||||||
[blog_post, blog_post]
|
[blog_post, blog_post]
|
||||||
end
|
end
|
||||||
|
|
@ -74,7 +76,7 @@ describe Grape::ActiveModelSerializers do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "uses namespace options when provided" do
|
it "uses namespace options when provided" do
|
||||||
subject.namespace :admin, :serializer => UserSerializer do
|
app.namespace :admin, :serializer => UserSerializer do
|
||||||
get('/jeff') do
|
get('/jeff') do
|
||||||
User.new(first_name: 'Jeff')
|
User.new(first_name: 'Jeff')
|
||||||
end
|
end
|
||||||
|
|
@ -83,18 +85,5 @@ describe Grape::ActiveModelSerializers do
|
||||||
get "/admin/jeff"
|
get "/admin/jeff"
|
||||||
last_response.body.should == "{\"user\":{\"first_name\":\"Jeff\",\"last_name\":null}}"
|
last_response.body.should == "{\"user\":{\"first_name\":\"Jeff\",\"last_name\":null}}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# [User2Serializer, 'user2', :user2].each do |serializer|
|
|
||||||
# it "should render using serializer (#{serializer})" do
|
|
||||||
# subject.get("/home", serializer: serializer) do
|
|
||||||
# User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'})
|
|
||||||
# end
|
|
||||||
|
|
||||||
# get "/home"
|
|
||||||
# last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue