From c599c489c3e2335c9cdff579802e92a5d9bd4859 Mon Sep 17 00:00:00 2001 From: Jonathan Richard Henry Evans Date: Thu, 29 Aug 2013 15:46:31 +0100 Subject: [PATCH] Cleaned up tests --- spec/grape_ams_spec.rb | 79 ++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 45 deletions(-) diff --git a/spec/grape_ams_spec.rb b/spec/grape_ams_spec.rb index 282b29a..69da288 100644 --- a/spec/grape_ams_spec.rb +++ b/spec/grape_ams_spec.rb @@ -1,49 +1,51 @@ require 'spec_helper' require 'spec_fakes' require "grape-active_model_serializers" -# require 'active_model' describe Grape::ActiveModelSerializers do - subject do - Class.new(Grape::API) - end + let(:app) { Class.new(Grape::API) } before do - subject.format :json - subject.formatter :json, Grape::Formatter::ActiveModelSerializers + app.format :json + app.formatter :json, Grape::Formatter::ActiveModelSerializers 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 - subject.get("/home/users", :serializer => UserSerializer) do + app.get("/home/users", :serializer => UserSerializer) do User.new end get("/home/users") last_response.headers["Content-Type"].should == "application/json" end - it "should infer serializer when there is no serializer set" do - subject.get("/home") do - User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'}) + context 'serializer is set to nil' do + before do + 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 - - get "/home" - last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}" end - it "should serializer arrays of objects" do - subject.get("/home") do + 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" + last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}" + end + end + + it "serializes arrays of objects" do + app.get("/home") do user = User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'}) [user, user] 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\"}]}" end - context "for models with compound names" do - it "should generate the proper 'root' node for individual objects" do - subject.get("/home") do + context "models with compound names" do + it "generates the proper 'root' node for individual objects" do + app.get("/home") do BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'}) end @@ -62,8 +64,8 @@ describe Grape::ActiveModelSerializers do last_response.body.should == "{\"blog_post\":{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"}}" end - it "should generate the proper 'root' node for serialized arrays of objects" do - subject.get("/home") do + it "generates the proper 'root' node for serialized arrays" do + app.get("/home") do blog_post = BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'}) [blog_post, blog_post] end @@ -74,7 +76,7 @@ describe Grape::ActiveModelSerializers do end it "uses namespace options when provided" do - subject.namespace :admin, :serializer => UserSerializer do + app.namespace :admin, :serializer => UserSerializer do get('/jeff') do User.new(first_name: 'Jeff') end @@ -83,18 +85,5 @@ describe Grape::ActiveModelSerializers do get "/admin/jeff" last_response.body.should == "{\"user\":{\"first_name\":\"Jeff\",\"last_name\":null}}" 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