{"action":"created","issue":{"url":"https://api.github.com/repos/rspec/rspec-rails/issues/1314","labels_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1314/labels{/name}","comments_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1314/comments","events_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1314/events","html_url":"https://github.com/rspec/rspec-rails/pull/1314","id":58099791,"number":1314,"title":"Patch mocks to properly handle JSON monkey patches","user":{"login":"cupakromer","id":1524496,"avatar_url":"https://avatars.githubusercontent.com/u/1524496?v=3","gravatar_id":"","url":"https://api.github.com/users/cupakromer","html_url":"https://github.com/cupakromer","followers_url":"https://api.github.com/users/cupakromer/followers","following_url":"https://api.github.com/users/cupakromer/following{/other_user}","gists_url":"https://api.github.com/users/cupakromer/gists{/gist_id}","starred_url":"https://api.github.com/users/cupakromer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cupakromer/subscriptions","organizations_url":"https://api.github.com/users/cupakromer/orgs","repos_url":"https://api.github.com/users/cupakromer/repos","events_url":"https://api.github.com/users/cupakromer/events{/privacy}","received_events_url":"https://api.github.com/users/cupakromer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-02-18T17:31:13Z","updated_at":"2015-02-18T21:30:15Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rspec/rspec-rails/pulls/1314","html_url":"https://github.com/rspec/rspec-rails/pull/1314","diff_url":"https://github.com/rspec/rspec-rails/pull/1314.diff","patch_url":"https://github.com/rspec/rspec-rails/pull/1314.patch"},"body":"Fix #1309\r\n\r\nRails adds a nearly global monkey patch to make converting objects into\r\nJSON easier. It adds both `to_json` and `as_json` methods on nearly all\r\ncore object types; including `Object` but not `BasicObject`.\r\n\r\nThat prevents a RSpec double or spy from working as expected. Since the\r\ndouble/spy object inherits these patches from `Object` they are not able\r\nto intercept them in the normal `method_missing` manner.\r\n\r\nWe work around that by applying a monkey patch directly on the RSpec\r\nmocks modules. This is necessary, instead of creating a new module and\r\nincluding it, because Ruby does not recognize new modules included on an\r\nexisting module for classes which have already included the base module.\r\n\r\nAdditionally, we must copy some of the `private` internals of the mock\r\nin order to preserve behavior. This is not a desirable approach but is\r\nnecessary.\r\n\r\n/cc @chav02 @JonRowe @myronmarston "},"comment":{"url":"https://api.github.com/repos/rspec/rspec-rails/issues/comments/74951955","html_url":"https://github.com/rspec/rspec-rails/pull/1314#issuecomment-74951955","issue_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1314","id":74951955,"user":{"login":"JonRowe","id":162976,"avatar_url":"https://avatars.githubusercontent.com/u/162976?v=3","gravatar_id":"","url":"https://api.github.com/users/JonRowe","html_url":"https://github.com/JonRowe","followers_url":"https://api.github.com/users/JonRowe/followers","following_url":"https://api.github.com/users/JonRowe/following{/other_user}","gists_url":"https://api.github.com/users/JonRowe/gists{/gist_id}","starred_url":"https://api.github.com/users/JonRowe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JonRowe/subscriptions","organizations_url":"https://api.github.com/users/JonRowe/orgs","repos_url":"https://api.github.com/users/JonRowe/repos","events_url":"https://api.github.com/users/JonRowe/events{/privacy}","received_events_url":"https://api.github.com/users/JonRowe/received_events","type":"User","site_admin":false},"created_at":"2015-02-18T21:30:15Z","updated_at":"2015-02-18T21:30:15Z","body":"> There's a fair bit of duplication for `as_json` vs `to_json` here.  Also, as you noted, this is coupled to rspec-mocks internals in a way that I'm uncomfortable with.\r\n\r\n> I think that we can solve both issues by defining some kind of public rspec-mocks API that would allow rspec-rails to add these methods to test doubles.  Maybe something like this:\r\n\r\n> ``` ruby\r\nRSpec::Mocks.configure do |c|\r\n  c.add_method_to_test_doubles(:as_json) { nil }\r\n  c.add_method_to_test_doubles(:to_json) { \"null\" }\r\nend\r\n```\r\n\r\n> Thoughts?\r\n\r\n:+1:"}}