[rspec-devel] Inverse Thinking, describing behaviors with contexts rather than in contexts

Zach Dennis zach.dennis at gmail.com
Sun Jun 22 13:25:54 EDT 2008


Two friday's ago I was talking to my pair Cabout a pet peeve I have
sometimes. Grouping behaviors by contexts, rather then the inverse.

So I ask you, in regards to describing behavior in a particular context,
have you ever entertained the idea of the describing behavior with a
particular context?

For example here is an example of describing behavior inside of a context:

  describe Trip
    context "with no breakfast expenses"
      it "has $0 for breakfast amount"
    end

    context "with breakfast expenses" do
      it "has a breakfast amount which is the sum total of breakfast
expenses"
    end
  end

And here's an example of describing the behavior with contexts:

  describe Trip
    behavior "computes breakfast amount" do
      context "with no breakfast expenses" do
        it "is $0"
      end

      context "with breakfast expenses" do
        it "is the sum of all breakfast expenses"
      end
    end
  end

The difference is subtle, but it inverts how behaviors are described. It
also groups contexts for a particular behavior together, rather than
grouping the behaviors to a particular context.

It does add another level of verbosity, but it makes my contexts more
meaningful because the setup is directly tied to the behavior I'm testing,
rather than potentially many behaviors which rely on the same setup.

I'm just thinking out loud at this point, but I'm interested in hearing your
thoughts.


-- 
Zach Dennis
http://www.continuousthinking.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-devel/attachments/20080622/a1f0e4b4/attachment-0001.html>


More information about the rspec-devel mailing list