开发者

Global mock with mocha

I have many tests for my class. When I added check for file existence, in my 开发者_JAVA百科class.

I needed to add this code in all my cases.

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

But I want declare a global mock for all my tests, can I make this with mocha and rspec?


Would do this like follows:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜