Forum: RSpec Putting shared specs in a separate file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ffb18df73b01724debcff6d030e417d2?d=identicon&s=25 LesFreeman (Guest)
on 2009-01-17 21:27
(Received via mailing list)
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
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2009-01-18 00:07
(Received via mailing list)
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: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

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 Wynne
http://blog.mattwynne.net
http://www.songkick.com
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-01-18 00:17
> 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.
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2009-01-18 01:45
(Received via mailing list)
On Sat, Jan 17, 2009 at 11:16 AM, LesFreeman <lesliefreeman3@gmail.com>
wrote:
> module ControllerAuthorizationSpecs
> [.rb]./).captures.first.camelize)
> `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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2009-01-18 01:54
(Received via mailing list)
On Sat, Jan 17, 2009 at 3:17 PM, Fernando Perez <lists@ruby-forum.com>
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
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2009-01-18 02:00
(Received via mailing list)
On 1/17/09 4:17 PM, Fernando Perez 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. ;)

-Ben
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2009-01-18 04:06
(Received via mailing list)
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: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

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
Ffb18df73b01724debcff6d030e417d2?d=identicon&s=25 LesFreeman (Guest)
on 2009-01-19 01:18
(Received via mailing list)
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
This topic is locked and can not be replied to.