0

i want to restructure my spec folder to something like below :

-spec
  -unit
    -Controller
      -example.rb
    -model
  -Integration
    -controller
    -model

What changes i have to make in my spec_helper.rb to run my specs in /spec/unit/controller/example.rb file.?

Has anybody ever tried restructuring specs based upon their high level like unit/integration etc.?

Manisha Awasthi
  • 449
  • 1
  • 5
  • 16

1 Answers1

0

You don't have to change anything in your spec_helper.rb. You do, however, need to change example.rb to example_spec.rb

This is the relevant line of code that implements this, in /gems/rspec-rails-2.14.1/lib/rspec/rails/tasks/rspec.rake:

namespace :spec do
  types = begin
    dirs = Dir['./spec/**/*_spec.rb'].
           map { |f| g=f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') ; puts ">>> Found #{g}."; g }.
           uniq.
           select { |f| File.directory?(f) }
    Hash[dirs.map { |d| [d.split('/').last, d] }]
  end

So all the text in the filename previous to _spec.rb is a convention - it doesn't change how Rails processes the files.


To run only some of your specs, you could filter them in the command line by tags or by text, or simply by giving the wanted root dir:

rspec . --example controller # only runs tests which have the word 'controller' in them
rsepc . --tag model # only runs tests which are tagged as 'model' => true
rspec ./spec/unit # only runs specs which are under ./spec/unit
Community
  • 1
  • 1
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • what changes i need to make to run my different group of specs? For ex : If i just want to run specs under ./spec/unit directory then what changes are required? – Manisha Awasthi Mar 11 '14 at 07:05
  • Added examples for selecting which specs to run – Uri Agassi Mar 11 '14 at 07:23
  • I copied my original spec/controller/example_controller_spec.rb to spec/unit/controller/example_controller_spec.rb and when i executed bundle exec rspec spec/unit/controller/example_controller_spec.rb it is failing with error 'undefined method for Sinatra::Application:Class' – Manisha Awasthi Mar 11 '14 at 11:54
  • do you require 'sinatra' in the spec_helper? do you require 'spec_helper' is the spec? – Uri Agassi Mar 11 '14 at 11:56
  • yes.. i require my spec_helper in spec. what i want to understand is - same spec runs if it's under spec/controller but it failes when it is under spec/unit/controller. what could be the possible reason? – Manisha Awasthi Mar 11 '14 at 11:57
  • perhaps you need to require_relative: `require_relative '../../spec_helper.rb'` – Uri Agassi Mar 11 '14 at 16:42