How to load a spec_helper.rb automatically in RSpec 2

后端 未结 2 865
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 02:20

When developing gems in Ruby, I almost always need a file in which I can configure RSpec to my needs and maybe before doing that, require some helper modules which should be ava

相关标签:
2条回答
  • 2021-01-31 02:34

    In RSpec 2, the /spec folder is always automatically on your load path. This means that all you need is:

    require 'spec_helper'
    

    at the top of your spec files. This will always load /spec/spec_helper.rb, and is the minimum you'll be able to get away with.

    This means you don't need a horrid approach such as:

    require File.join(File.dirname(File.dirname(__FILE__)), 'spec_helper.rb')
    

    (which needs to be updated for different nesting levels).

    Also you can add to your .rspec file the option: --require spec_helper, which will require this file in each spec file, without the manual require statement at the top.

    0 讨论(0)
  • 2021-01-31 02:47

    The --require spec_helper line is automatically added to the .rspec file for RSpec 3.0 when you do rspec --init.

    0 讨论(0)
提交回复
热议问题