0

简单地说,我有一个带有调用类方法的实例方法的类,我想使用 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。谢谢!

4

1 回答 1

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
于 2012-08-08T03:11:27.497 回答