{"action":"created","issue":{"url":"https://api.github.com/repos/rspec/rspec-rails/issues/1357","labels_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1357/labels{/name}","comments_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1357/comments","events_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1357/events","html_url":"https://github.com/rspec/rspec-rails/issues/1357","id":70411098,"number":1357,"title":"Dynamic method verification fails when method stubbed using allow_any_instance_of on a class not previously instantiated.","user":{"login":"tmertens","id":2243386,"avatar_url":"https://avatars.githubusercontent.com/u/2243386?v=3","gravatar_id":"","url":"https://api.github.com/users/tmertens","html_url":"https://github.com/tmertens","followers_url":"https://api.github.com/users/tmertens/followers","following_url":"https://api.github.com/users/tmertens/following{/other_user}","gists_url":"https://api.github.com/users/tmertens/gists{/gist_id}","starred_url":"https://api.github.com/users/tmertens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tmertens/subscriptions","organizations_url":"https://api.github.com/users/tmertens/orgs","repos_url":"https://api.github.com/users/tmertens/repos","events_url":"https://api.github.com/users/tmertens/events{/privacy}","received_events_url":"https://api.github.com/users/tmertens/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":11,"created_at":"2015-04-23T13:26:18Z","updated_at":"2015-11-02T11:51:18Z","closed_at":null,"body":"Dynamic method verification fails when method stubbed using allow_any_instance_of on a class not previously instantiated.\r\n\r\nGiven a test stubs a method on a class using `allow_any_instance_of`, and `verify_partial_doubles` is enabled, rspec-rails will fail to resolve the dynamic method if it is stubbed via `any_instance` prior to any instance(s) of that class being instantiated.\r\n\r\n**Environment**\r\n\r\n* ruby 2.1.5\r\n* rails (3.2.19)\r\n* rspec-core (3.2.3)\r\n* rspec-expectations (3.2.1)\r\n* rspec-mocks (3.2.1)\r\n* rspec-rails (3.2.1)\r\n* rspec-support (3.2.2)\r\n\r\n**Steps To Reproduce:**\r\n* Create a rails app\r\n* Generate a model with some fields: `rails g model Article title:string text:text`\r\n* Add the following test:\r\n\r\n```ruby\r\nrequire 'rails_helper'\r\n\r\nRSpec.describe 'dynamic methods' do\r\n  it 'does not resolve dynamic instance methods on first call to any_instance' do\r\n    allow_any_instance_of(Article).to receive(:title).and_return 'foo'\r\n    a = Article.new\r\n    expect(a.title).to eql 'foo'\r\n  end\r\nend\r\n```\r\n\r\n* Migrate and run the test.  It will fail with the message:\r\n\r\n```\r\nRSpec::Mocks::MockExpectationError: Article does not implement #title\r\n./spec/models/any_instance_spec.rb:5:in `block (2 levels) in <top (required)>'\r\n-e:1:in `load'\r\n-e:1:in `<main>'\r\n```\r\n\r\n* Change `Article` in the stub to `Article.new.class`:\r\n\r\n```ruby\r\n  it 'does not resolve dynamic instance methods on first call to any_instance' do\r\n    allow_any_instance_of(Article.new.class).to receive(:title).and_return 'foo'\r\n    a = Article.new\r\n    expect(a.title).to eql 'foo'\r\n  end\r\n```\r\n\r\n* Run the test again.  It will pass."},"comment":{"url":"https://api.github.com/repos/rspec/rspec-rails/issues/comments/152997225","html_url":"https://github.com/rspec/rspec-rails/issues/1357#issuecomment-152997225","issue_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1357","id":152997225,"user":{"login":"mhenrixon","id":106795,"avatar_url":"https://avatars.githubusercontent.com/u/106795?v=3","gravatar_id":"","url":"https://api.github.com/users/mhenrixon","html_url":"https://github.com/mhenrixon","followers_url":"https://api.github.com/users/mhenrixon/followers","following_url":"https://api.github.com/users/mhenrixon/following{/other_user}","gists_url":"https://api.github.com/users/mhenrixon/gists{/gist_id}","starred_url":"https://api.github.com/users/mhenrixon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhenrixon/subscriptions","organizations_url":"https://api.github.com/users/mhenrixon/orgs","repos_url":"https://api.github.com/users/mhenrixon/repos","events_url":"https://api.github.com/users/mhenrixon/events{/privacy}","received_events_url":"https://api.github.com/users/mhenrixon/received_events","type":"User","site_admin":false},"created_at":"2015-11-02T11:51:18Z","updated_at":"2015-11-02T11:51:18Z","body":":+1: "}}