[rspec-users] alias method spec?

Bob Cotton bob.cotton at rallydev.com
Wed Mar 21 08:34:01 EDT 2007

"Bryan Helmkamp" <bhelmkamp at gmail.com> writes:
> 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?

We have this exact problem, and the solution I was kicking around was:

Module Foo
  def same; end

Module Bar
  def same; end

context Foo, Bar, "Foo and Bar" do
  setup do

where 'context' would accept a list of modules to include, and
generate a new context for each.


More information about the rspec-users mailing list