[rspec-users] Custom Example Groups

Brandon Olivares programmer2188 at gmail.com
Sun Apr 19 09:50:14 EDT 2009



> -----Original Message-----
> From: rspec-users-bounces at rubyforge.org [mailto:rspec-users-
> bounces at rubyforge.org] On Behalf Of David Chelimsky
> Sent: Sunday, April 19, 2009 8:59 AM
> To: rspec-users
> Subject: Re: [rspec-users] Custom Example Groups
> 
> You should be able to get what you're after like this:
> 
> A class method:
> 
> class MyGroup < Spec::ExampleGroup
>   class << self
>     def with_method(verb)
>       subject { send(verb, description_args.first) }
>       yield
>     end
>   end
> end
> 
> This is a bit tricky, but basically description_args is an array of
> all the arguments sent to describe() (including in nested groups). The
> first one, in your example above, is '/posts'. So the result of this
> code is that when you call with_method(:get), the subject becomes the
> result of calling get '/posts'.
> 
> Give that a shot and let us know if it works.
> 
> Also - you really don't need custom groups for this sort of thing,
> because you can define with_method in a module and extend the
> configuration w/ that module:
> 
>   module WithMethod
>     def with_method(verb)
>       subject { send(verb, description_args.first) }
>       yield
>     end
>   end
> 
>   Spec::Runner.configure {|c| c.extend(WithMethod)}
> 

Thanks. I'm not sure I want to actually do a request though, because it's
really only testing the routes in that example.

I know there are already ways of spec'ing the routes of course, but I'm
trying to learn more about customizing RSpec. And I really like this syntax.

So as a more expanded version, let's say this:

describe '/posts' do
  with_method :get do
    it { should be_accepted }
    it { should map_to_action 'index' }
  end
  
  with_method :post do
    it { should be_accepted }
    it { should map_to_action 'create' }
  end
  
  with_method :put do
  it { should_not be_accepted }
  end

  with_method :delete do
  it { should_not be_accepted }
  end
end

Thanks,
Brandon

> Cheers,
> David
> 
> >
> > Thanks,
> > Brandon
> >
> >> Cheers,
> >> David
> >>
> >> >
> >> > Pat
> >> >
> >> > On Sat, Apr 18, 2009 at 11:31 PM, Brandon Olivares
> >> > <programmer2188 at gmail.com> wrote:
> >> >> Hi,
> >> >>
> >> >> I want to build a custom example group, but there really aren't
> any
> >> examples
> >> >> anywhere for how to do so. The new chapter in The RSpec Book
> talks
> >> about it,
> >> >> but doesn't actually show an example, only how to register it.
> >> >>
> >> >> Looking at rspec-rails, I can see a pattern, but I don't know if
> >> that's
> >> >> specific to rspec-rails, or if it is what should be done. In
> rspec-
> >> rails, it
> >> >> has:
> >> >>
> >> >> Class << self
> >> >>
> >> >> Within the class, and all the methods are within that.
> >> >>
> >> >> So, are there any examples anywhere for how to write an example
> >> group?
> >> >>
> >> >> Also, if I set an instance variable within the example group,
> will
> >> the
> >> >> matchers specific to that group be able to access it? That'll
> >> simplify my
> >> >> design a bit.
> >> >>
> >> >> Thanks,
> >> >> Brandon
> >> >>
> >> >> _______________________________________________
> >> >> rspec-users mailing list
> >> >> rspec-users at rubyforge.org
> >> >> http://rubyforge.org/mailman/listinfo/rspec-users
> >> >>
> >> > _______________________________________________
> >> > rspec-users mailing list
> >> > rspec-users at rubyforge.org
> >> > http://rubyforge.org/mailman/listinfo/rspec-users
> >> >
> >> _______________________________________________
> >> rspec-users mailing list
> >> rspec-users at rubyforge.org
> >> http://rubyforge.org/mailman/listinfo/rspec-users
> >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list