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
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/
-Ben
That was post was gold, Ben. It cleared up most of my questions and
confusion regarding #include, #extend, and mix-ins. Thanks, mate.
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
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:
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.