Ruby 摩卡全球模拟

Ruby 摩卡全球模拟,ruby,rspec,ruby-mocha,Ruby,Rspec,Ruby Mocha,我班上有很多考试。 当我在类中添加文件存在性检查时 我需要在我的所有案例中添加此代码 File.any_instance. expects(:exist?). with('test_file'). returns(true). once() 但是我想为我的所有测试声明一个全局模拟,我可以用mocha和rspec来实现吗?会这样做: describe Thing do # If this is really done once... before :a

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

我需要在我的所有案例中添加此代码

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

但是我想为我的所有测试声明一个全局模拟,我可以用mocha和rspec来实现吗?

会这样做:

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