Skip to content

Commit

Permalink
Avoid comparing JSON strings directly.
Browse files Browse the repository at this point in the history
On Ruby 1.8.7, where key insertion order is not guaranteed,
this leads to intermittent test failures.
  • Loading branch information
Artem Chistyakov authored and temochka committed Jan 9, 2019
1 parent 1cafa71 commit a502eb7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
4 changes: 4 additions & 0 deletions spec/support/custom_matchers.rb
Expand Up @@ -28,3 +28,7 @@ def postmark_json?(str)
postmark_json?(actual)
end
end

RSpec::Matchers.define :json_representation_of do |x|
match { |actual| Postmark::Json.decode(actual) == x }
end
42 changes: 19 additions & 23 deletions spec/unit/postmark/api_client_spec.rb
Expand Up @@ -800,9 +800,9 @@
end

it 'performs a POST request to /templates with the given attributes' do
expected_json = {'Name' => 'template name'}.to_json

expect(http_client).to receive(:post).with('templates', expected_json).and_return(response)
expect(http_client).to receive(:post).
with('templates', json_representation_of('Name' => 'template name')).
and_return(response)

template = subject.create_template(:name => 'template name')

Expand All @@ -822,9 +822,9 @@
end

it 'performs a PUT request to /templates with the given attributes' do
expected_json = {'Name' => 'template name'}.to_json

expect(http_client).to receive(:put).with('templates/123', expected_json).and_return(response)
expect(http_client).to receive(:put).
with('templates/123', json_representation_of('Name' => 'template name')).
and_return(response)

template = subject.update_template(123, :name => 'template name')

Expand Down Expand Up @@ -880,13 +880,12 @@
end

it 'performs a POST request and returns unmodified suggested template model' do
expected_template_json = {
'HtmlBody' => '{{MyName}}',
'TextBody' => '{{MyName}}',
'Subject' => '{{MyName}}'
}.to_json

expect(http_client).to receive(:post).with('templates/validate', expected_template_json).and_return(response)
expect(http_client).to receive(:post).
with('templates/validate',
json_representation_of('HtmlBody' => '{{MyName}}',
'TextBody' => '{{MyName}}',
'Subject' => '{{MyName}}')).
and_return(response)

resp = subject.validate_template(:html_body => '{{MyName}}',
:text_body => '{{MyName}}',
Expand Down Expand Up @@ -929,13 +928,11 @@
end

it 'performs a POST request and returns validation errors' do
expected_template_json = {
'HtmlBody' => '{{#each}}',
'TextBody' => '{{MyName}}',
'Subject' => '{{MyName}}'
}.to_json

expect(http_client).to receive(:post).with('templates/validate', expected_template_json).and_return(response)
expect(http_client).
to receive(:post).with('templates/validate',
json_representation_of('HtmlBody' => '{{#each}}',
'TextBody' => '{{MyName}}',
'Subject' => '{{MyName}}')).and_return(response)

resp = subject.validate_template(:html_body => '{{#each}}',
:text_body => '{{MyName}}',
Expand All @@ -952,12 +949,11 @@

describe "#deliver_with_template" do
let(:email) {Postmark::MessageHelper.to_postmark(message_hash)}
let(:email_json) {Postmark::Json.encode(email)}
let(:http_client) {subject.http_client}
let(:response) {{"MessageID" => 42}}

it 'converts message hash to Postmark format and posts it to /email/withTemplate' do
expect(http_client).to receive(:post).with('email/withTemplate', email_json) {response}
expect(http_client).to receive(:post).with('email/withTemplate', json_representation_of(email)) {response}
subject.deliver_with_template(message_hash)
end

Expand All @@ -970,7 +966,7 @@
end

it 'converts response to ruby format' do
expect(http_client).to receive(:post).with('email/withTemplate', email_json) {response}
expect(http_client).to receive(:post).with('email/withTemplate', json_representation_of(email)) {response}
r = subject.deliver_with_template(message_hash)
r.should have_key(:message_id)
end
Expand Down

0 comments on commit a502eb7

Please sign in to comment.