Putting shared specs in a separate file


#1

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:ingem_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:inrequire’
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:ingem_original_require’
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in
require' ... 6 levels... from ./spec/controllers/../spec_helper.rb:7:ineach’
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


#2

On 17 Jan 2009, at 19:16, LesFreeman wrote:

module ControllerAuthorizationSpecs
[.rb]./).captures.first.camelize)
gem_original_require' from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:inrequire’
… 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:inrequire’
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

I suggest you post a comment on the blog post where you got this idea
from, because it seems incomplete to me - I can’t make sense of it.

Some of us who have been using RSpec for a while have found that using
‘macros’ to generate examples dynamically is often a better technique
for re-using specs. You might want to check out this post:

http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/

Matt W.
http://blog.mattwynne.net
http://www.songkick.com


#3

http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/

Too bad the font and colors are completely unreadable by human eyes. How
do you handle such websites? I remember on Mac there is an app that can
invert colors of the display.


#4

On Sat, Jan 17, 2009 at 11:16 AM, LesFreeman removed_email_address@domain.invalid
wrote:

module ControllerAuthorizationSpecs
[.rb]./).captures.first.camelize)
gem_original_require' from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:inrequire’
… 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:inrequire’
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 _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


#5

On 1/17/09 4:17 PM, Fernando P. wrote:

http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/

Too bad the font and colors are completely unreadable by human eyes. How
do you handle such websites? I remember on Mac there is an app that can
invert colors of the display.

LOL… Sorry about that! I actually totally agree with you. I am in
the middle of a redesign of my blog and I haven’t settled on my colors
yet and so what you see is some basic default color scheme.
I’ve changed it to a mostly white color scheme now so it should be more
readable by human eyes. :wink:

-Ben


#6

On Sat, Jan 17, 2009 at 3:17 PM, Fernando P. removed_email_address@domain.invalid
wrote:

http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/

Too bad the font and colors are completely unreadable by human eyes. How
do you handle such websites?

I bumped up the font and I can read it fine. Also, I’m human.

I remember on Mac there is an app that can invert colors of the display.

ctrl+option+command+8 will do that

Pat


#7

Hi all,
Thanks for the great replies.

This one was a total dunce cap mistake. I had somehow gotten the
include for the shared files in before the include for rspec. Duh.

I’m interested in the macros idea. I’ll have to look into that more.

Thanks again,
Les


#8

On 2009-01-17, at 14:16, LesFreeman wrote:

module ControllerAuthorizationSpecs
[.rb]./).captures.first.camelize)
gem_original_require' from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:inrequire’
… 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:inrequire’
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

Hi Les. What I do is put this stuff into modules in spec/shared/ , and
use #include to bring in the example methods, and #extend to bring in
the example group methods. For example:
http://gist.github.com/48492

Does that help at all?
Nick