[rspec-devel] Proposal: when_it syntax

Antti Tarvainen antti.tarvainen at iki.fi
Tue Oct 7 12:06:05 EDT 2008

> I propose a new syntax that helps DRY up specs when using
> one-expectation-per-example style. There have been similar proposals
> before, but no patches. As far as I know, the idea wasn't killed, just
> forgotten.

Thanks for the feedback, everyone.

I added the ability for when_it to accept descriptions, so you can write:

describe PoemsController do
  when_it "responds to GET index" do
    get :index
  # ...

(See http://gist.github.com/15300 for the entire file.)

- David: I agree that hunting the causes of fails will be more
difficult with this syntax. It may be easier to keep the spec up to
date though through refactorings. It's the same old battle between
readability and DRY.

- David and Zach, regarding the names "when_it" and ":implicitly": I
like "when_it", since it describes the intention well, especially with
the desciptions as in the above example. ":Implicitly" I don't like,
but I couldn't think of a better word.

- Zach: Yes, at least for description blocks with lots of examples, it
is less confusing to write preconditions in a single example.

- Zach: I haven't tried this in any real project yet. I intend to do
that in a small internal project.

- Aslak: You are right. All this does is remove the :act method call
from the examples.

- David: I think extension API is a good idea. Would rspec-rails use it?

I agree with you that "when_it" should probably not be added to RSpec,
at least not now.


More information about the rspec-devel mailing list