-
-
Save zorab47/f00e4ad8d5b02b583c2ba97db6011f67 to your computer and use it in GitHub Desktop.
Pact issue with Blueprinter and Oj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
It produces the error: