# Mixing in spec helper methods

**URL:** <https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090>\
**Category:** RSpec\
**Created:** [November 12, 2008, 1:25am UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090 "2008-11-12T01:25:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 12, 2008, 1:25am UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/1 "2008-11-12T01:25:57Z")

</div>

I’ve written a module for my specs that contains a helper method, and  
am mixing the module into my specs with #include . It seems that the  
method must be called with an #it block. If it isn’t, this error occurs:

… in `method_missing': undefined method `it\_should\_behave\_like\_an\_action\_that\_sets\_the\_flash\_notice’ for  
Spec::Rails::Example::ControllerExampleGroup::Subclass\_1:Class  
(NoMethodError) from ./spec/controllers/properties\_controller\_spec.rb:8

Is there a way to call my helper method from outside of an #it block?  
Here’s the code:  
[http://pastie.org/312680](http://pastie.org/312680)

Thanks!  
Nick

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 12, 2008, 1:31am UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/2 "2008-11-12T01:31:48Z")

</div>

Nick H. wrote:

> Here’s the code:  
> [http://pastie.org/312680](http://pastie.org/312680)
> 
> Thanks!  
> Nick
> 
> * * *
> 
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)  
> It looks like you want those methods in your module as class method, not  
> instance methods. Try extending, not including, your module (extend  
> AuthSpecHelpers.) You might want to read my post about creating rspec  
> macros as well:  
> [http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/](http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/)

-Ben

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 12, 2008, 1:40am UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/3 "2008-11-12T01:40:00Z")

</div>

Nick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Is there a way to call my helper method from outside of an #it block?  
> Here’s the code:  
> [http://pastie.org/312680](http://pastie.org/312680)

You want extend instead of include.

Also, if you want to share this among many example groups, you can do it  
in the config block instead of extending it in every single example  
group:

Spec::Runner.configure do |config|  
config.extend AuthSpecHelpers  
end

You can even do it for particular example group types if you want:

Spec::Runner.configure do |config|  
config.extend AuthSpecHelpers, :type =\> :controller  
end

Pat

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 12, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/4 "2008-11-12T21:45:42Z")

</div>

On 2008-11-11, at 19:31, Ben M. wrote:

> > properties\_controller\_spec.rb:8  
> > not instance methods. Try extending, not including, your module  
> > (extend AuthSpecHelpers.) You might want to read my post about  
> > creating rspec macros as well:  
> > [http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/](http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/)
> 
> -Ben

That was post was gold, Ben. It cleared up most of my questions and  
confusion regarding #include, #extend, and mix-ins. Thanks, mate.

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 12, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/5 "2008-11-12T21:56:53Z")

</div>

On 2008-11-11, at 19:39, Pat M. wrote:

> > ./spec/controllers/properties\_controller\_spec.rb:8  
> > group:
> 
> Pat

That’s convenient. What I’ve done is add this to the beginning:  
require ‘spec/shared/auth\_spec\_helpers’

and add this to the ‘config’ block:  
config.extend AuthSpecHelpers, :type =\> :controller

of spec\_helper.rb . Is that a suitable, or preferred, method of  
extending the helper methods in the AuthSpecHelpers module to  
controller specs?

Thanks,  
Nick

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 12, 2008, 1:59am UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/6 "2008-11-12T01:59:54Z")

</div>

On Tue, Nov 11, 2008 at 6:39 PM, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > (NoMethodError) from  
> > group:
> 
> Spec::Runner.configure do |config|  
> config.extend AuthSpecHelpers  
> end
> 
> You can even do it for particular example group types if you want:
> 
> Spec::Runner.configure do |config|  
> config.extend AuthSpecHelpers, :type =\> :controller  
> end

Note that config.extend was only added recently, in rspec-1.1.9.

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 12, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/7 "2008-11-12T21:59:16Z")

</div>

On 2008-11-11, at 19:49, David C. wrote:

> David  
> Thanks for remembering that I’m using an old version of RSpec and  
> RSpec-Rails, mate 😉 That was enough of a reason to upgrade, so now  
> I’m running 1.1.11 of the plugins. Hoorah!  
> -Nick

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 12, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/8 "2008-11-12T22:09:17Z")

</div>

On 2008-11-11, at 19:31, Ben M. wrote:

> > properties\_controller\_spec.rb:8  
> > [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)  
> > It looks like you want those methods in your module as class method,  
> > not instance methods. Try extending, not including, your module  
> > (extend AuthSpecHelpers.) You might want to read my post about  
> > creating rspec macros as well:  
> > [http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/](http://www.benmabey.com/2008/06/08/writing-macros-in-rspec/)
> 
> -Ben

After digesting that post again, especially the contents of  
#self.included , I noticed that one could alternatively do this:

module AssignMacro  
def self.extended(receiver)  
receiver.extend ExampleGroupMethods  
receiver.send :include, ExampleMethods  
end  
end

describe UserPhotosController do  
describe “GET ‘users/1/photos/2’” do  
extend AssignMacro  
# …  
end  
end

Is one approach more preferable? Or should one do this so that both  
“include AssignMacro” and “extend AssignMacro” give the same, desired,  
result?:

module AssignMacro  
def self.included(receiver)  
receiver.extend ExampleGroupMethods  
receiver.send :include, ExampleMethods  
end

class \<\< self  
alias\_method :extended, :included  
end  
end

Thanks for your input, guys.  
-Nick

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 13, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/9 "2008-11-13T19:52:30Z")

</div>

On 2008-11-12, at 16:38, Ben M. wrote:

> > 
> 
> would be:  
> macors.. likes so:  
> module’s ExampleMethods and ExampleGroupMethods modules respectively.
> 
> The ‘use\_macro’ is probably more clear than the hooks. Just a  
> thought.
> 
> -Ben
> 
> 1. [http://olabini.com/blog/2008/09/evil-hook-methods/](http://olabini.com/blog/2008/09/evil-hook-methods/)

The “evil hook methods” article explained exactly how I was feeling.  
Everyone’s different, but I feel dirty extending the receiver in  
#included , and vice versa. I’ve decided to go along with your first  
suggested alternative:

## spec/spec\_helper.rb

# ..snip..

Spec::Runner.configure do |config|

# ..snip..

config.extend AuthSpecHelpers::ExampleGroupMethods, :type  
=\> :controller  
config.include AuthSpecHelpers::ExampleMethods, :type  
=\> :controller  
end

Thanks again for taking the time to explain all of this, Ben. I really  
learned a lot. Cheers,  
Nick

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [November 12, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/mixing-in-spec-helper-methods/153090/10 "2008-11-12T22:38:38Z")

</div>

Nick H. wrote:

> > > (NoMethodError) from ./spec/controllers/properties\_controller\_spec.rb:8  
> > > [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)
> 
> ```
> # ...
> receiver.send :include, ExampleMethods
> 
> ```
> 
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

Some people would say that using the included or extended module hooks  
like that are evil.[1] That said it is a very common ruby idiom even if  
it is bad. If you are going to use the evil idiom then I would suggest  
using include and not extend since it is the idiom. 🙂

Another way to do it that wouldn’t take advantage of the evil hooks  
would be:

describe UserPhotosController do  
describe “GET ‘users/1/photos/2’” do  
extend AssignMacro::ExampleGroupMethods  
include AssignMacro::ExampleMethods  
# …  
end

Of course that is somewhat annoying to type everywhere. So perhaps you  
could add another example group method that would mix in macors.. likes  
so:

describe UserPhotosController do  
describe “GET ‘users/1/photos/2’” do  
use\_macro AssignMacro  
# …  
end  
end

The ‘use\_maco’ would be a module that you mix in onto all rspec’s  
example group methods that would simply include and extend the given  
module’s ExampleMethods and ExampleGroupMethods modules respectively.

The ‘use\_macro’ is probably more clear than the hooks. Just a thought.

-Ben

1. [http://olabini.com/blog/2008/09/evil-hook-methods/](http://olabini.com/blog/2008/09/evil-hook-methods/)
