let is not limited to context block
rebelweb opened this issue · comments
I am not sure if there is a reason for this, but when I have a let state in a context block. I am coming from the RSpec world and have done this in the past with RSpec. If this isn't a recommended practice I understand. I have an example below, to better explain.
class UserControllerTest < ActionController::TestCase
describe '#create' do
context 'with valid attributes' do
let(:action) { post :create, user: {name: 'Ryan'} }
#valid attribute tests go here
end
context 'with valid attributes' do
let(:action) { post :create, user: {name: 'Ted'} } #overwrites the first let statement in this block since they have the same name
#invalid attribute tests go here
end
end
end
I don't see any issues with the code you posted, except in minitest you would just use more describe
blocks since there is no context
. For example:
class UserControllerTest < ActionController::TestCase
describe '#create' do
describe 'with valid attributes' do
let(:action) { post :create, user: {name: 'Ryan'} }
# Valid attribute tests go here.
end
describe 'with valid attributes' do
let(:action) { post :create, user: {name: 'Ted'} } #overwrites the first let statement in this block since they have the same name
# Invalid attribute tests go here.
end
end
end
What problem are you having with this code?
Okay thank you I didn't realize context blocks were unavailable. The issue I was having was that when I describe my let in the second context it any test in the first context was using the second let from the second context. Everything is working as expected since I moved my context statement to a describe. Thank You.
Cool, glad I could help. A bit of advice which is mostly personal, try to avoid deep nested describe contexts. The first one per action seems totally legit, but overly nesting might appear to DRY things up but would overly abstract and cause one to build up context in the worst way IMO, scrolling. For example, consider making all your let
statements within the #create
context. Name one action_valid
and other other action_invalid
. Just my 2¢ :) and I hope that helps. Thanks for using this gem!
Thank You for the advice and creating the gem.