spec/lib/shamu/json_api/response_spec.rb in shamu-0.0.4 vs spec/lib/shamu/json_api/response_spec.rb in shamu-0.0.5

- old
+ new

@@ -1,17 +1,24 @@ require "spec_helper" +require "active_model" describe Shamu::JsonApi::Response do let( :context ) { Shamu::JsonApi::Context.new } let( :response ) { Shamu::JsonApi::Response.new context } it "uses presenter if given" do - presenter = double Shamu::JsonApi::Presenter - expect( presenter ).to receive( :present ) do |_, builder| - builder.identifier :response, 9 - end.with( anything, kind_of( Shamu::JsonApi::ResourceBuilder ) ) + presenter = double + expect( presenter ).to receive( :new ) do |resource, builder| + instance = Shamu::JsonApi::Presenter.new resource, builder + expect( instance ).to receive( :present ) do + builder.identifier :response, 9 + end + + instance + end + response.resource double, presenter end it "expects a block if no presenter" do expect do @@ -36,7 +43,30 @@ it "includes errors" do response.error NotImplementedError.new expect( response.compile ).to include errors: [ hash_including( code: "not_implemented" ) ] + end + + it "writes validation errors" do + klass = Class.new do + include ActiveModel::Validations + + attr_reader :title + validates :title, presence: true + + validate :general_problem + + def general_problem + errors.add :base, "nope" + end + end + + allow( klass ).to receive( :name ).and_return "Example" + record = klass.new + record.valid? + + response.validation_errors record.errors + expect( response.compile ).to include errors: include( hash_including( source: { pointer: "/data/attributes/title" } ) ) # rubocop:disable Metrics/LineLength + expect( response.compile ).to include errors: include( hash_including( source: { pointer: "/data" } ) ) end end \ No newline at end of file