意外调用:__mock_proxy()在运行包含mock_model的rspec规范时

时间:2012-04-10 16:17:42

标签: ruby-on-rails rspec rspec-rails

我的很多规范都失败了,并给了我一个关于__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)>'

我正在运行Rails 3.1.4和Ruby 1.9.2,并且正在使用这些宝石:

$ 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)

我发现有几个人收到相同错误的报告,包括this one on github,但它们似乎都已过时或者没有包含解决方案。


编辑:这些规格适用于我的同事的计算机,但不适用于我的计算机。看起来差异与最后包含的RSpec::Rails::MochaRSpec::Rails::Mocks订单有关 - RSpec::Rails::Mocha必须包括在内。

我做了一个测试规范,其输出显示:

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

在我的同事的计算机上(规格工作),它产生了这个输出:

#<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"]

在我的电脑上(规格不起作用),它产生了这个输出:

#<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"]

1 个答案:

答案 0 :(得分:0)

通过将:require => false添加到Gemfile中的rspec-rails-mocha行来解决此问题。