我试图用rspec来模拟一个类的方法:
LIB / db.rb
class Db def self.list(options) Db::Payload.list(options) end end
LIB /分贝/ payload.rb
class Db::Payload def self.list(options={}) end end
在我的规格中,我试图设置期望当调用Db.list时,Db :: Payload.list将被调用:
require 'db/payload' describe Db do before(:each) do @options = {} Db::Payload.should_receive(:list).with(@options) end it 'should build the LIST payload' do Db.list(@options) end end
问题是我总是收到以下错误:
undefined method `should_receive' for Db::Payload:Class
任何帮助了解这个错误将是最感激的:-)
解决方法
你的spec_helper.rb应该有这样的东西:
Spec::Runner.configure do |config| # == Mock Framework # # RSpec uses its own mocking framework by default. If you prefer to # use mocha,flexmock or RR,uncomment the appropriate line: # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr end
默认参数是config.mock_with:rspec,它启用了should_receive方法.例如,如果您使用摩卡车,相当于预期,所以请确保使用正确的嘲笑框架.