rspec - Is calling the method I'm testing in a before block bad practice? -
# first "should render china's jobs page country_name china" 'show', country: 'china' expect(assigns(:title)).to eq("#{location.country} tech jobs") expect(response).to be_success end # second context "with country name" before { 'show', country: 'china' } { expect(assigns(:title)).to eq("#{location.country} tech jobs") } { expect(response).to be_success } end
which 1 think better? prefer second 1 if before { 'show' }
not bad practice.
i wouldn't it's bad practice, have downsides:
- it's slower. in second case, it's got 2 examples, each of runs
get 'show', country: 'china'
-- can cause noticeable slowness whenget 'show'
slow. - it produces documentation output (to me @ least) doesn't describe behavior well. compare
--format doc
output 2 cases. - it makes harder add setup additional examples may add in future. consider if there's future case need test needs particular records inserted in db work properly. because action being tested (
get 'show')
inbefore
block rather in example body, can't add additional setup 1 example needs it. (you can, of course, add additional setupbefore
block itself, exacerbate slowness issue).
there benefits using before
block. makes easy follow "one expectation per example" guideline. in turn give separate failing or passing examples assigns
, response status expectations, can make more clear precisely failing, example.
which use depends on tradeoffs want make. tend towards first example (no before
block).
on side note, if use latter form, i'd use specify
alias (rather it
) because use it
when doc string or matcher reads english expression off of it
:
specify { expect(assigns(:title)).to eq("#{location.country} tech jobs") } specify { expect(response).to be_success }
Comments
Post a Comment