全球模拟与摩卡

时间:2011-08-24 15:12:21

标签: ruby rspec mocha

我班上有很多考试。 当我在班上添加检查文件存在时。

我需要在所有情况下添加此代码。

File.any_instance.
    expects(:exist?).
    with('test_file').
    returns(true).
    once()

但是我想为我的所有测试声明一个全局模拟,我能用mocha和rspec做这个吗?

1 个答案:

答案 0 :(得分:0)

这样做会如下:

describe Thing do

  # If this is really done once...

  before :all do
    File.any_instance.expects(:exist?).with('test_file').returns(true).once
  end

  # If this is done once per example...

  before :each do
    File.any_instance.expects(:exist?).with('test_file').returns(true).once
  end

  # ...

end
相关问题