jimweirich / rspec-given

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

Using Given/When/Then side-by-side with before/it? #47

Open ronen opened 9 years ago

ronen commented 9 years ago

I've found that depending on what I'm testing in some cases I might prefer the traditional rspec context/it flow, and in other cases Given/When/Then flow. Or I might want to gradually migrate a file full of tests from one to the other, so while in transition the file might have some of each kind.

It would be nice if the README could document whether & how it's OK to do something like:

describe "Thing" do
  context "traditional" do
    before(:each) { setup something }
    it "works" do
       try something
       expect to get the proper result
    end
  end
  context "newfangled" do 
    Given { setup other }
    When { try other }
    Then { get the other result }
  end
end

That seems like it would work (and I think it does), but things seems like they'd get messy if there was a Given nested inside a context with a before or vice versa. (Or maybe there's a well-defined way in which it can work?) Though I imagine before(:suite) hooks wouldn't be a problem.

Anyway, WBN for rspec-given to take a firm stand on what's expected to work.

searls commented 9 years ago

In rspec-given@1.x, this was not encouraged but worked perfectly well because there was no interplay between the given/when/then methods.

In 2.x there may be subtle problems depending on what is combined, particularly in cases where a before or let raises error and is in turn only invoked by a Then. But honestly, the nuance needed to keep them working is mostly wasted as long as folks keep the given and traditional DSLs to mostly-separate contexts (which works fine)

ronen commented 9 years ago

Oh, your response beat me to it -- i was just about to close this and repost on the new repo. WIll continue discussing here...

as long as folks keep the given and traditional DSLs to mostly-separate contexts (which works fine)

So should the README have a short section such as:

## Combining Given/When/Then with the traditional before/let/it DSL

Given/When/Then clauses and traditional rspec DSL blocks can coexist in the same suite,
as long they are in separate contexts, without any shared `before(:each)` or `let` blocks.

Shared `before(:all)` and `before(:suite)` blocks are fine and will apply normally.

Is that last sentence true?

searls commented 9 years ago

I waffle on whether it's worth addressing in that light in the README. :+meh: for now

ronen commented 9 years ago

Clearly not a huge deal. But I'd lean towards including something like it for two reasons:

searls commented 9 years ago

I think the latter is a compelling point we need to make, but I'm wary of saying it loudly and proudly until we really understand the constraints on the former

ronen commented 9 years ago

Oh... I thought it was just me who didn't understand :) In that light, yeah I'd agree hold off proclaiming anything that ain't necessarily so.