[rspec-users] alias method spec?

Bryan Helmkamp bhelmkamp at gmail.com
Tue Mar 20 18:50:12 EDT 2007

On 3/20/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> On 3/20/07, Bryan Helmkamp <bhelmkamp at gmail.com> wrote:
> > I've experienced a similar situation before. The reason I looked at
> > testing that they are the same method is that it seemed more DRY to do
> > that than to copy/paste the specs for that method and replace the
> > name. WDYT?
> That makes sense. Another way to keep the code DRY would be to wrap
> the example in a loop:
> (pardon the trunk syntax....)
> describe Thing do
>   [:b, :c].each do |method|
>     it "should do a when you send it #{method}" do
>       #expect something....
>       Thing.new.send method
>     end
>   end
> end
> This way the same behaviour get specified for both methods, but
> there's only one set of specs describing the behaviour.

I like that solution, David. I've used loops for defining specify
block in some other places, and I'm happy with the results. Just
wondering, if instead of having one Thing with two methods that are
identical, you instead had two types of things (Foo and Bar) with one
method that should behave the same for each, how would you go about
specifying that? Would you write a high level custom matcher? Use a
loop to create two context blocks? Or something else?


More information about the rspec-users mailing list