Last active
May 3, 2016 04:28
-
-
Save lpadukana/e167548181b8516fbfbe to your computer and use it in GitHub Desktop.
Rspec custom argument matcher
This file contains hidden or 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
| 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