Skip to content

Instantly share code, notes, and snippets.

@lpadukana
Last active May 3, 2016 04:28
Show Gist options
  • Select an option

  • Save lpadukana/e167548181b8516fbfbe to your computer and use it in GitHub Desktop.

Select an option

Save lpadukana/e167548181b8516fbfbe to your computer and use it in GitHub Desktop.
Rspec custom argument matcher
class Hash
def deep_include?(sub_hash)
sub_hash.keys.all? do |key|
self.has_key?(key) && if sub_hash[key].is_a?(Hash)
self[key].is_a?(Hash) && self[key].deep_include?(sub_hash[key])
else
self[key] == sub_hash[key]
end
end
end
end
module JsonIncluding
class JsonIncluding
def initialize(json)
@json = json
end
def ==(other_json)
JSON.parse(other_json).deep_include?(JSON.parse(@json))
end
def inspect
"#{@json.to_s}"
end
end
def json_including(json)
JsonIncluding.new(json)
end
end
RSpec.configure do |config|
config.include JsonIncluding
end
describe 'blah' do
it "updates the applicant in connection-orders" do
json = ({applicant: {first_name: nil}}).to_json
expect(client).to receive(:update).with(json_including(json), 1234)
order.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment