[rspec-users] rspec 1 - nested examples (in an each block) use the final block variable 4 times instead of each of the 4 once

Ashley Moran ashley.moran at patchspace.co.uk
Fri Jul 30 18:13:04 EDT 2010

On 30 Jul 2010, at 10:00 PM, nruth wrote:

> http://gist.github.com/501296
> I don't think anything needs to change, though a wrapper function
> (each + an inner describe) might help flag it as a possible pitfall.

Hi Nick

I think the "before + ivar" pattern (below) is on its way out.  At least, I consider it deprecated in my head:

  describe "before" do
    before(:each) { @foo = "bar" }
    before(:each) { @foo = "baz" }
    it "does this" do
      @foo.should eq "baz"

But the new syntax does indeed let you redefine `let` blocks.
  describe "let" do
    let(:foo) { "bar" }
    let(:foo) { "baz" }
    it "does this (currently)" do
      foo.should eq "baz"

Arguably, this is not the most desirable behaviour, as it leads to silent/confusing failures, as you've seen.  Maybe it would be better if this raised an error?  (Which is only an option with `let`, not with ivars.)

Also, when combined with shared examples, this could conceivably be used to violate the Liskov Substitution Principle (if the LSP applies to specs).  While Ruby as a language lets you redefine stuff at will, I'm not sure that's appropriate here.  I can't imagine wanting to redefine a let, instead of factoring it out properly.  Anyone got any thoughts on that?



More information about the rspec-users mailing list