jimweirich / rspec-given

Given/When/Then keywords for RSpec Specifications
https://github.com/jimweirich/rspec-given
MIT License
654 stars 61 forks source link

Adding support for When -> And, Given -> And #21

Closed jetaggart closed 11 years ago

jetaggart commented 11 years ago

I've been using rpsec-given in capybara acceptance tests. I find myself doing something like this:

Given { admin_logged_in }
Given { visit admin_dashboard_page }

When { click_on "New person" }
When { fill_in "Name", with: "A name" }

Then { page.has_content? "Person created." }
And { page.has_content? "A name" }

I find myself wanting to do:

Given { admin_logged_in }
And { visit admin_dashboard_page }

When { click_on "New person" }
And { fill_in "Name", with: "A name" }

Then { page.has_content? "Person created." }
And { page.has_content? "A name" }

I know I can use do; end blocks, but I find this much nicer to read.

Is adding support for Given -> And, When -> And worth it? Or am I missing the point?

jimweirich commented 11 years ago

See https://github.com/jimweirich/rspec-given/pull/1 for a discussion of this issue.

After that discussion, I did add And to the GIven/When/Then lexicon, but its not a synonym, it has its own semantics (similar to Then, but without rerunning the setup code).

jetaggart commented 11 years ago

That makes a lot of sense.

One question though. In the case of the acceptance test, the subsequent Given's/When's MUST be run in order (top down). Is there any reason to introduce it for order dependent Given's and When's?

Or would you prefer this style over the previous example?:

Given do
  admin_logged_in
  visit admin_dashboard_page
end
jimweirich commented 11 years ago

Non-lazy Givens will run in order from first to last, outermost to innermost scope. After the non-lazy Givens, the Whens will be run in the same order. This is guaranteed by the framework.

Maybe I'm not understanding the question.

jetaggart commented 11 years ago

Your comment sounds like the order is not guaranteed:

"(1) And implies sequential processing or some kind of ordering. The lazy givens can actually be executed in any order."

In my case, I want that sequential processing. Your last response answered my question.

I'm just trying to figure out the best way to use rspec-given :) Thanks for the responses.

jimweirich commented 11 years ago

This https://github.com/jimweirich/rspec-given#execution-ordering gives the details on order guarantees.