Michael Durrant
Michael Durrant

Reputation: 96484

Ruby - rspec how to include specs in root of project?

rspec always finds files in the spec/ directory tree.

How can I have it also find files that are in the root of the project (the one that contains the spec/ folder iself.

For example if I have a small project with only two tests, any folders may essentially be unneeded overhead given a minimalist approach. Or if you only have one type of test and would only create one folder it may not (or may) be worth creating that one folder or just omitting it altogether. The value it adds of course is immediate description of what type of tests exist, even if only one type exist.

Upvotes: 0

Views: 558

Answers (2)

David Maze
David Maze

Reputation: 158977

Spec supports a --default-path option, and you can put a default set of command-line options into an .rspec file. That is, you can create a .rspec file in your project root directory containing

--default-path .

My experience has generally been that rspec tests are almost always in a spec directory; I would be a little surprised to see them in a top-level directory. This in particular mirrors the standard gem filesystem layout which puts all library code into a lib directory and tests in a parallel spec directory.

Upvotes: 1

Michael Durrant
Michael Durrant

Reputation: 96484

One approach:

# File: spec_helper.rb  Add the following at the top:

Dir.glob(File.expand_path("../../*_spec.rb", __FILE__)).each do |file|
  require file
end

This will include files at the root to the ones already being included in spec/

Upvotes: 0

Related Questions