[rspec-devel] More DRY in descriptions

Evgeny evgeny.zislis at gmail.com
Wed May 16 06:32:47 EDT 2007


Greetings All,

I have been writing some code with rSpec (0.9.x) for a new project,
and I notice that there is a thing I keep repeating myself on.

Best shown in a short example:

describe ClassNameForSomeUser, "when first created" do
  it "should fail when no name provided" do
    lambda { ClassNameForSomeUser.new(:name => nil) }.should
raise_error(ArgumentError, "No name provided")
  end
  it "should fail when something else is wrong" do
    lambda { ClassNameForSomeUser.new(:wrongparam => true) }.should
raise_error SomethingBad
  end
end

naturally there are many more descriptions in there, so to make it just a
little bit more DRY I did this:

describe ClassNameForSomeUser, "when first created" do
  before(:each) do
    @UCN = ClassNameForSomeUser
  end
  it "should fail when no name provided" do
    lambda { @UCN.new(:name => nil) }.should raise_error(ArgumentError, "No
name provided")
  end
  it "should fail when something else is wrong" do
    lambda { @UCN.new(:wrongparam => true) }.should raise_error SomethingBad
  end
end

But then I notice that when I write "describe" I specify the class name
already, so why not use it in some way?
Is it possible to do something like :

describe ClassNameForSomeUser, "when first created" do
  it "should fail when no name provided" do
    lambda { this_class.new(:name => nil) }.should
raise_error(ArgumentError, "No name provided")
  end
  it "should fail when something else is wrong" do
    lambda { this_class.new(:wrongparam => true) }.should raise_error
SomethingBad
  end
end

is this possible, or anything similar?

Regards,

- evgeny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20070516/1dc89938/attachment.html 


More information about the rspec-devel mailing list