Search code examples
unit-testingrspecrspec2jrubyonrails

Recommended way to structure rspec modules?


I have a rails app, plus code in lib. I have the spec directory under RAILS_ROOT.

How should I put my tests in spec?

Currently, I am thinking of the following:

spec/lib

spec/controllers

spec/models

Further, I do some common setup / use common steps (e.g., generate an invalid user) in many tests. Where do you recommend I put the modules that do the common setup /steps in my rspec tests?


Solution

  • Your proposed directory structure is fine.

    As for your helper modules, a common idiom is for these to go in the spec/support directory. You can include them all automatically by placing the following code into your spec_helper.rb file:

    Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
    

    You could just place the code directly in spec_helper.rb itself, but that can get messy and they could be wiped out by regenerating the helper file.