[rspec-users] Mixing in spec helper methods

Nick Hoffman nick at deadorange.com
Wed Nov 12 15:56:20 EST 2008


On 2008-11-11, at 19:39, Pat Maddox wrote:
> Nick Hoffman <nick at deadorange.com> writes:
>> 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
>
> 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

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


More information about the rspec-users mailing list