Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Problems customizing spec directory structure #600

Closed
amartinsn opened this Issue Aug 22, 2012 · 4 comments

Comments

Projects
None yet
4 participants

Hi guys,

I'm trying to come up with a different testing structure for my project where I can define three types of specs (unit, integration and acceptance), so under spec directory I created these 3 subdirectories. The goal is to run my specs as fast as possible, each type with its own spec_helper.rb file, with the minimum necessary to run the specs for that type. And also to be able to run something like rake spec:unit, rake spec:integration or rake spec:acceptance. So with this goal, my directory structure looks like the following:

spec
|=> unit (tests only domain logic, no persistence or integration points)
|=> integration (tests the integration with persistence mechanisms, message bus, etc.)
|=> acceptance (tests user interaction, with capybara)

Inside the unit directory I created the default subdirectories (models, controllers, helpers, views, etc.) with the proper specs inside each of them. For example, I created a spec called application_controller_spec.rb, inside spec/unit/controllers. The problem is when I try to run this spec, by executing the command (rspec spec/unit/controllers/application_controller_spec.rb) I get the following error:

undefined method controller' for #<Class:0x007f8f0a6e3610> (NoMethodError) from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:202:inmodule_eval'
from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:202:in subclass' from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:187:indescribe'
from /Users/amartins/Development/projects/receitas/spec/unit/controllers/application_controller_spec.rb:49:in block in <top (required)>' from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:202:inmodule_eval'
from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:202:in subclass' from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:187:indescribe'
from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/dsl.rb:18:in describe' from /Users/amartins/Development/projects/receitas/spec/unit/controllers/application_controller_spec.rb:5:in<top (required)>'
from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:699:in load' from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:699:inblock in load_spec_files'
from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:699:in map' from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:699:inload_spec_files'
from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22:in run' from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:inrun_in_process'
from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in run' from /Users/amartins/.rvm/gems/ruby-1.9.3-p194@receitas/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:inblock in autorun'

It doesn't find the reference to the controller method, from rspec-rails, and also any other method like 'get', 'put', 'response', etc.
I tried to follow the piece of code where the error is happening, but couldn't find the solution. Is there a configuration I need to do, to be able to customize my directory structure?

Thanks!
~Alex

Owner

myronmarston commented Aug 22, 2012

Hey Alex,

I like the approach you're taking...it's actually the way I've been doing it on several projects recently, although I haven't tried it on a rails project yet.

The integration rspec-rails provides for the various spec types is based on modules that get included example groups based on the convention of which directory the spec files are in. Have a look at the example folder to see all the modules that get included each spec type. example.rb wires it all up based on the file_path.

You'll need to do this wiring yourself since you're outside of the "normal" conventions for rspec-rails. You can either include the modules manually in each example group, use metadata to tag the example groups with their type, and include based on the type, or use a similar file_path convention approach as rspec-rails does.

Contributor

alindeman commented Aug 24, 2012

Is there anything actionable that RSpec itself can do here? :)

Thanks for your help Myron, still haven't tried the solution you proposed but will close the issue, and as soon as I get it done will post it here for feedback.

@amartinsn amartinsn closed this Aug 24, 2012

robb1e commented Jan 5, 2013

Hi,

I'm trying to do the same thing and poking around the docs it seems you can add a type tag to your description and it behaves as if it's in the directory.

https://www.relishapp.com/rspec/rspec-rails/v/2-12/docs/controller-specs

I've successfully made this change in a Rails app. It looks like the change was probably made in 2.4 at least by going on the documentation. The top of each file looks like:

describe FooHelper, type: :helper do
describe FooController, type: controller do
describe FooModel, type: model do
etc

My directory structure is:

spec/
  unit/
  integration/
  etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment