测试实例方法调用类方法

时间:2012-08-07 23:54:38

标签: ruby-on-rails rspec

简单地说,我有一个带有调用类方法的实例方法的类,我想使用RSpec测试在运行实例方法时调用类方法。所以,例如

class Test
  def self.class_method
    #do something
  end

  def instance_method
    #do stuff
    self.class.class_method
  end
end

在RSpec中,我尝试过Test.should_receive(:class_method),但这似乎做了一些奇怪的事情导致我的测试返回奇怪的行为。如果这是我应该使用的,也许RSpec已经过时了?我正在使用RSpec 2.7.0。谢谢!

1 个答案:

答案 0 :(得分:1)

如果你真的不关心类方法正在做什么,只有它被调用,你可以这样做:

describe Test do
  context '#instance_method' do
    it 'should call the class method' do
      Test.should_receive(:class_method)
      Test.new.instance_method
    end
  end
end