[rspec-devel] [ rspec-Feature Requests-5625 ] specification reuse: specify & amp; amp; amp; context & amp; amp; amp; helper methods

noreply at rubyforge.org noreply at rubyforge.org
Thu Oct 26 17:33:26 EDT 2006


Feature Requests item #5625, was opened at 2006-09-04 19:19
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=5625&group_id=797

>Category: runner module
Group: None
>Status: Closed
Priority: 3
Submitted By: Suraj Kurapati (snk)
>Assigned to: David Chelimsky (dchelimsky)
Summary: specification reuse: specify & context & helper methods

Initial Comment:
Hello,

I'm using BDD via RSpec to develop and verify hardware. My specifications tend to be long and sequential, such as:

An empty cache
- should miss when read

A read miss on an empty cache
- should be passed to a lower-level cache

A read miss response from a lower-level cache to an empty cache
- should cause the empty cache to respond to the miss

In these situations, the setup() for each context is determined by the conditions present when the previous context/specify() has finished. For example, it is similar to a specification like this:

1 + 1
- should be 2

2 + 5
- should be 7

7 + 0.99
- should be 7.99

See the pattern?


I could use helper methods to minimize duplication of logic, but... there is still duplication of variables. So, here are three ideas which can simplify reuse of the conditions present at the _end_ of a specify() as a context for new specify()s. I like the first one best.

## nested specify() statements are evaluated as encountered by their parent.

context "1 + 1" do
  setup do
    @sum = 1 + 1
  end

  specify "should be 2" do
    @sum.should_equal 2

    specify "should be 7" do
      @sum += 5
      @sum.should_equal 7

      specify "should be 7.99" do
        @sum += 0.99
        @sum.should_equal 7.99
      end
    end
  end
end


## specify() returns its block, which can then be invoked elsewhere.

context "1 + 1" do
  setup do
    @sum = 1 + 1
  end

  s1 = specify "should be 2" do
    @sum = 1 + 1
    @sum.should_equal 2
  end

  s2 = specify "should be 7" do
    s1.call

    @sum += 5
    @sum.should_equal 7
  end

  s3 = specify "should be 7.99" do
    s2.call

    @sum += 0.99
    @sum.should_equal 7.99
  end
end


## Rake-style prerequisites in specifications

context "1 + 1" do
  setup do
    @sum = 1 + 1
  end

  specify "should be 2" do
    @sum = 1 + 1
    @sum.should_equal 2
  end

  specify "should be 7" => ["should be 2"] do
    @sum += 5
    @sum.should_equal 7
  end

  specify "should be 7.99" => ["should be 7"] do
    @sum += 0.99
    @sum.should_equal 7.99
  end
end


Thanks for your consideration.

----------------------------------------------------------------------

>Comment By: Aslak Hellesoy (aslak_hellesoy)
Date: 2006-10-26 17:33

Message:
Based on previous discussions we have decided not to implement this feature. See http://butunclebob.com/ArticleS.DavidChelimsky.SpecOrganization for more detail

----------------------------------------------------------------------

Comment By: Suraj Kurapati (snk)
Date: 2006-09-04 19:32

Message:
I just want to emphasize that what is needed is a way to
reuse the conditions present at the end of a specify() as
the conditions present at the start of another specify().

My ideas on how this will look when impemented don't really
matter. I'm sure that you RSpec developers can think of a
nicer ways to augment the DSL.

Thanks for your consideration.

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=5625&group_id=797


More information about the rspec-devel mailing list