On Thu, Dec 31, 2009 at 12:54 PM, Phillip Koebbe <phillipkoe...@gmail.com>wrote:

> I have a module of custom examples, such as
>
> module ControllerHelperMethods
>        module MyExampleGroupMethods
>                def should_set_the_body_id(body_id)
>                        it "should set the body id to '#{body_id}'" do
>                                assigns[:body_id].should == body_id
>                        end
>                end
>        end
>
>        def self.included(receiver)
>                receiver.extend MyExampleGroupMethods
>        end
> end
>
> and in spec_helper:
>
> Spec::Runner.configure do |config|
>        config.include(ControllerHelperMethods, :type => :controllers)
> end
>

First of all, you can simplify this by using extend() instead of include()
on the config object:

module ControllerMacros
  def should_set_the_body_id(body_id)
    it "should set the body id to '#{body_id}'" do
      assigns[:body_id].should == body_id
    end
  end
end

Spec::Runner.configure do |config|
  config.extend(ControllerMacros, :type => :controllers)
end

Note that we generally referred to as "macros," so I recommend using that
name for consistency.


> I have duplicate code in two of my custom examples, so I want to
> refactor it to a private method, but I can't seem to get the private
> method to work. I have tried the most obvious way (private keyword
> followed by the method), and also a couple of more clever approaches
> that involved doing an instance_eval in self.included.
>

I *think* that if you use extend instead of include + included hook, you can
just use the private keyword and all will be well.

HTH,
David


> First, should I be able to call private methods in my custom examples?
> If so, how.
>
> Peace.
> Phillip
>
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to