Skip to content

Instantly share code, notes, and snippets.

@zorab47
Last active August 15, 2024 17:29
Show Gist options
  • Save zorab47/f00e4ad8d5b02b583c2ba97db6011f67 to your computer and use it in GitHub Desktop.
Save zorab47/f00e4ad8d5b02b583c2ba97db6011f67 to your computer and use it in GitHub Desktop.
Pact issue with Blueprinter and Oj
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
# Activate the gem you are reporting the issue against.
# gem "oj", "3.11.3" # test ok
gem "oj", "3.16.0" # test fail
gem "activesupport"
gem "pact"
gem "pact-support"
gem "rspec"
gem "httparty"
end
require "active_support"
require "active_support/json"
require "json"
require "pact"
require "oj"
require "pact/consumer/rspec"
Pact.service_consumer "Zoo App" do
has_pact_with "Animal Service" do
mock_service :animal_service do
port 1234
end
end
end
require "httparty"
class Alligator
attr_reader :name
def initialize name
@name = name
end
def == other
other.is_a?(Alligator) && other.name == name
end
end
class AnimalServiceClient
include HTTParty
base_uri "http://animal-service.com"
def get_alligator
name = JSON.parse(self.class.get("/alligator").body)['name']
Alligator.new(name)
end
end
require "rspec/autorun"
RSpec.describe AnimalServiceClient, :pact => true, :order => :defined do
before do
# Configure your client to point to the stub service on localhost using the port you have specified
AnimalServiceClient.base_uri "localhost:1234"
end
let(:body) { { name: Pact.like("Betty") } }
subject { AnimalServiceClient.new }
describe "get_alligator" do
it "returns a alligator" do
animal_service.given("an alligator exists")
.upon_receiving("a request for an alligator")
.with(method: :get, path: "/alligator", query: "")
.will_respond_with(
status: 200,
headers: {"Content-Type" => "application/json"},
body: body
)
expect(subject.get_alligator).to eq(Alligator.new("Betty"))
end
context "rendering via Oj.generate prior" do
before do
Oj.generate({ name: "Boop" })
end
it "does not set create_additions by default" do
expect(Oj.default_options).to include(create_additions: false)
end
it "returns a alligator" do
animal_service
.given("an alligator exists one")
.upon_receiving("a request for an alligator")
.with(method: :get, path: "/alligator", query: "")
.will_respond_with(
status: 200,
headers: {"Content-Type" => "application/json"},
body: body
)
expect(subject.get_alligator).to eq(Alligator.new("Betty"))
end
context "and setting create_additions" do
before do
Oj.generate({ name: "Boop" })
Oj.default_options = { create_additions: true }
end
it "returns a alligator" do
animal_service
.given("an alligator exists one")
.upon_receiving("a request for an alligator again")
.with(method: :get, path: "/alligator", query: "")
.will_respond_with(
status: 200,
headers: {"Content-Type" => "application/json"},
body: body
)
expect(subject.get_alligator).to eq(Alligator.new("Betty"))
end
end
end
context "with Oj" do
xit "returns a alligator" do
pending "fails regardless of Oj version"
Oj.optimize_rails()
animal_service
.given("an alligator exists two")
.upon_receiving("a request for an alligator")
.with(method: :get, path: "/alligator", query: "")
.will_respond_with(
status: 200,
headers: { "Content-Type" => "application/json" },
body: body
)
expect(subject.get_alligator).to eq(Alligator.new("Betty"))
end
end
end
end
@zorab47
Copy link
Author

zorab47 commented May 3, 2021

It produces the error:

Failures:

  1) AnimalServiceClient get_alligator rendering a Blueprint JSON before returns a alligator
     Failure/Error: expect(subject.get_alligator).to eq(Alligator.new("Betty"))

       expected: #<Alligator:0x00007f81d7aeeda8 @name="Betty">
            got: #<Alligator:0x00007f81d7aeeec0 @name={"json_class"=>"Pact::SomethingLike", "contents"=>"Betty"}>

       (compared using ==)

       Diff:
       @@ -1,2 +1,3 @@
       -#<Alligator:0x00007f81d7aeeda8 @name="Betty">
       +#<Alligator:0x00007f81d7aeeec0
       + @name={"json_class"=>"Pact::SomethingLike", "contents"=>"Betty"}>

@bethesque
Copy link

Ok, I have been a professional Ruby developer for 10+ years now, and I did not know you could inline a gemfile declaration! That is going to come in handy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment