Using expect and should in RSpec

rspec, ruby, ruby-on-rails

Solution

I think this is a good example

describe "Car"
  describe '#valid?' do
    context 'when its name is nil' do
      let(:car) { FactoryGirl.create(:car, :name => nil) }

      it 'is not valid' do 
        expect(car).to_not be_valid
      end
    end
  end
end

Find more about better specs here

Problem

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.

Original source