Rachel Shallit
Rachel Shallit

Reputation: 2010

Unexpected invocation: __mock_proxy() when running rspec specs that include mock_model

A lot of my specs are failing and giving me an error message about __mock_proxy():

$ rspec spec/helpers/liaisons_helper_spec.rb
WARNING: Cucumber-rails required outside of env.rb.  The rest of loading is being defered until env.rb is called.
  To avoid this warning, move 'gem cucumber-rails' under only group :test in your Gemfile
FF

Failures:

  1) LiaisonsHelper hrefs_of_email_addresses_for_liaison to enumerate email_addresses as RESTful links
     Failure/Error: email_address = mock_model(EmailAddress)
     Mocha::ExpectationError:
       unexpected invocation: #<Mock:EmailAddress_1001>.__mock_proxy()
       unsatisfied expectations:
       - expected exactly once, not yet invoked: #<Mock:EmailAddress_1001>.id(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:EmailAddress_1001>.blank?(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:EmailAddress_1001>.valid?(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:EmailAddress_1001>.marked_for_destruction?(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:EmailAddress_1001>.destroyed?(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:EmailAddress_1001>.persisted?(any_parameters)
     # ./spec/helpers/liaisons_helper_spec.rb:6:in `block (3 levels) in <top (required)>'

  2) LiaisonsHelper hrefs_of_phone_numbers_for_liaison to enumerate phone_numbers as RESTful links
     Failure/Error: phone_number = mock_model(PhoneNumber)
     Mocha::ExpectationError:
       unexpected invocation: #<Mock:PhoneNumber_1002>.__mock_proxy()
       unsatisfied expectations:
       - expected exactly once, not yet invoked: #<Mock:PhoneNumber_1002>.id(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:PhoneNumber_1002>.blank?(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:PhoneNumber_1002>.valid?(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:PhoneNumber_1002>.marked_for_destruction?(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:PhoneNumber_1002>.destroyed?(any_parameters)
       - expected exactly once, not yet invoked: #<Mock:PhoneNumber_1002>.persisted?(any_parameters)
     # ./spec/helpers/liaisons_helper_spec.rb:19:in `block (3 levels) in <top (required)>'

I'm running Rails 3.1.4 and Ruby 1.9.2, and am using these gems:

$ bundle list | grep mocha
  * mocha (0.10.5)
  * rspec-rails-mocha (0.3.1)
$ bundle list | grep rspec
  * rspec (2.8.0)
  * rspec-apotomo (0.9.6)
  * rspec-cells (0.1.2)
  * rspec-core (2.8.0)
  * rspec-expectations (2.8.0)
  * rspec-mocks (2.8.0)
  * rspec-rails (2.8.1)
  * rspec-rails-mocha (0.3.1)

I've found several reports of people getting the same error, including this one on github, but they all either seem to be out of date or don't include a solution.


Edit: These specs work on my coworker's computer but don't work on mine. It looks like the difference has to do with what order RSpec::Rails::Mocha and RSpec::Rails::Mocks are included in - RSpec::Rails::Mocha has to be included last.

I made a test spec whose output shows this:

describe 'test' do
  it 'test' do
    m = self.method(:mock_model)
    p m
    p m.source_location
    p self.class.included_modules.map(&:to_s).grep(/Moc/)
  end
end

On my coworker's computer (where the specs work), it produced this output:

#<Method: RSpec::Core::ExampleGroup::Nested_1::Nested_1(RSpec::Rails::Mocha)#mock_model>
["/Users/username/.rvm/gems/ruby-1.9.2-p290-patched@development3/gems/rspec-rails-mocha-0.3.1/lib/rspec/rails/mocha.rb", 59]
["RSpec::Rails::Mocha", "RSpec::Rails::Mocks", "RSpec::Core::MockFrameworkAdapter", "Mocha::API", "Mocha::ParameterMatchers", "Mocha::ObjectMethods"]

And on my computer (where the specs don't work), it produced this output:

#<Method: RSpec::Core::ExampleGroup::Nested_1::Nested_1(RSpec::Rails::Mocks)#mock_model>
["/Users/username/.rvm/gems/ruby-1.9.2-p290-patched@development3/gems/rspec-rails-2.9.0/lib/rspec/rails/mocks.rb", 67]
["RSpec::Rails::Mocks", "RSpec::Rails::Mocha", "RSpec::Core::MockFrameworkAdapter", "Mocha::API", "Mocha::ParameterMatchers", "Mocha::ObjectMethods"]

Upvotes: 0

Views: 1484

Answers (1)

Rachel Shallit
Rachel Shallit

Reputation: 2010

This is fixed by adding :require => false to the rspec-rails-mocha line in the Gemfile.

Upvotes: 0

Related Questions