4

例如,我有 A 类。

class A
end

并希望在规范中从存根方法返回该类的实例。

A.any_instance.stub(:my_method).and_return(<here is the same instance on which my_method is called should got>)

有可能在 RSpec 中做类似的事情吗?

4

1 回答 1

1

这将为您解决问题:

A.any_instance.stub(:my_method) do |*args|
    instance = RSpec::Mocks::space.send(:receivers).last
end

我从这里的 rspec 代码中挖出了这个:rspec github code

PS:这完全取决于 rspec 实现,并且将来可能会改变。背景有点复杂,在这里解释一下。我将来可能会添加。

于 2012-10-19T20:13:49.873 回答