[rspec-users] Putting shared specs in a separate file

Pat Maddox pergesu at gmail.com
Sat Jan 17 18:52:07 EST 2009


On Sat, Jan 17, 2009 at 11:16 AM, LesFreeman <lesliefreeman3 at gmail.com> wrote:
> Hello,
> I am trying to move shared specs out of my spec_helper and into
> separate files. I found an article about the process here:http://
> blog.kineticweb.com/articles/2008/04/15/automagical-rspec-shared-
> example-loading-from-separate-files
>
> Essentially, I created a spec/shared directory, then a file in that
> dir called controller_authorization_specs.rb
>
> #controller_authorization_specs.rb
> module ControllerAuthorizationSpecs
>  shared_examples_for "test" do
>  end
> end
>
> Then, in my spec helper, some code to load the shared spec files:
> #spec_helper.rb
> ...
> Dir[File.dirname(__FILE__)+'/**/shared/*'].each { |group|
>  require group
>  include Object.const_get(group.match(/.*[\/]{1}([\w]*)
> [.rb]./).captures.first.camelize)
> }
> ...
>
>
> Which seems dandy. But, I get these kind of errors:
> /Users/lesfreeman/Sites/equvents/spec/shared/
> controller_authorization_specs.rb:4: undefined method
> `shared_examples_for' for ControllerAuthorizationSpecs:Module
> (NoMethodError)
>        from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in
> `gem_original_require'
>        from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in
> `require'
>        from /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/
> active_support/dependencies.rb:155:in `require'
>        from /Users/lesfreeman/Sites/equvents/spec/spec_helper.rb:11
>        from /Users/lesfreeman/Sites/equvents/spec/spec_helper.rb:10:in
> `each'
>        from /Users/lesfreeman/Sites/equvents/spec/spec_helper.rb:10
>        from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in
> `gem_original_require'
>        from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in
> `require'
>         ... 6 levels...
>        from ./spec/controllers/../spec_helper.rb:7:in `each'
>        from ./spec/controllers/../spec_helper.rb:7
>        from spec/controllers/sessions_controller_spec.rb:1:in `require'
>        from spec/controllers/sessions_controller_spec.rb:1
>
> I'm sure there's an easy solution, but I'm scratching my head. Any
> help would be greatly appreciated

Yeah, I do this.  I put shared specs in <foo>_behavior.rb.

Are you doing these requires after you require rspec?

Also, I greatly prefer shared behaviors over macros for stuff like
this.  Macros make sense for very generic stuff that you reuse across
specs that are otherwise unrelated.  Shared behaviors are for
organizing facets of behavior.

Pat


More information about the rspec-users mailing list