Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created September 12, 2012 12:17
Show Gist options
  • Save jimweirich/3706244 to your computer and use it in GitHub Desktop.
Save jimweirich/3706244 to your computer and use it in GitHub Desktop.
Description of the "And" Experimental feature for RSpec/Given (was "Also")
# Experimental And Clauses
#
# I've been thinking about tests where there are multiple Thens
# in a single context. All the Givens and before blocks are run
# for each Then. Since Then's are idempotent, that seems to be a waste.
#
# Maybe we can introduce And clauses. They are like Then clauses, but
# they reuse the Givens of an existing Then clause. This could make a
# difference in spec that have expensive setups.
#
# Example:
context "with one item" do
Given(:initial_contents) { [:an_item] }
context "when popping" do
When(:pop_result) { stack.pop }
Then { pop_result.should == :an_item }
And { stack.depth.should == 0 }
end
end
# Some Limitations
#
# (1) And clauses are run after the first Then clause in a context. It
# is an error to have an Also clause without a Then.
#
# (2) And clauses are not independent, a failure in a Then or an early
# And clause will cause the remaining And clauses to _not_ be run.
#
# Limitation 2 is a technical limitation. Essentially the And clauses are running
# in the same "spec/test" as the its parent Then. It would be nice if there
# was a way to report multiple spec failures in a single spec run.
#
# RSpec/Given 2.1.0.beta.4 has this experimental feature. Try it with:
#
# gem install --pre rspec-given
@jonatas
Copy link

jonatas commented Sep 12, 2012

I really miss them as a rspec user. +1

@edavey
Copy link

edavey commented Sep 12, 2012

yes please and I would also go for the "And" keyword.

@gigasquid
Copy link

I think it woud be a great addition

@mockra
Copy link

mockra commented Sep 12, 2012

Definitely a big fan of the addition.

@jimweirich
Copy link
Author

I make the explicit choice to use Also rather than And because I felt the semantics were different from Cucumber's And. After consideration (and weighing all the comments) I don't think the difference will be confusing, so we will go with "And" for the name of the clause.

Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment