[rspec-users] Mixing in spec helper methods
Nick Hoffman
nick at deadorange.com
Wed Nov 12 16:08:46 EST 2008
On 2008-11-11, at 19:31, Ben Mabey wrote:
> Nick Hoffman wrote:
>> 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
>>
>> Thanks!
>> Nick
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> 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/
>
> -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
More information about the rspec-users
mailing list