Reputation: 12189
There is the following RSpec code:
it 'is not valid if name is not present' do
@car.name = nil
expect(@car).to be_invalid
end
I read "Testing with RSpec" by Aaron Sumner now, and he writes about new styles in RSpec. Earlier I wrote the following code:
it 'is not valid if name is not present' do
@car.name = nil
it { should_not be_valid }
end
Please, tell me, do I right? Thanks.
Upvotes: 5
Views: 11997
Reputation: 37409
Use the let
API instead of the using @car
, so that you won't risk changing the state from test to test, in a way which might break tests in unpredictable manner.
The it
one-liner refers to the subject
so to make your second style work it should look like that:
describe '#valid?' do
let(:car) do
# make your car here
end
subject { car } # <- this is the important line to make it work...
before do
car.name = nil
end
it { should_not be_valid }
end
Upvotes: 3