Michael Durrant
Michael Durrant

Reputation: 96454

rspec - "after it" - how to change from should to expect syntax?

How can I change from should to expect for the it in this code:-

context 'class_name' do
  before do
    Fabricator(:some_company, :class_name => Company) do
      name 'Hashrocket'
    end
  end
  after { Fabrication.clear_definitions }
  its(:name) { should == 'Hashrocket' }
  it { should be_kind_of(Company) }
end

I can see that the its will probably be:

 expect(name.to eq 'Hashrocket')

but what should the it { should be_kind_of(Company) } become given the implicit subject.
Would it be

  expect(it).to be_kind_of(Company) 

?

I don't have the project set up yet from github (it is large).

Upvotes: 0

Views: 175

Answers (1)

Shepmaster
Shepmaster

Reputation: 430476

There's no reason to change the short-form it blocks to the expect syntax. One of the reasons for the new syntax is to avoid the monkeypatching required to add should to every object. The short-form it blocks already avoid this problem.

However, if you need to access the implicit subject, you can say

expect(subject).to be_kind_of(Company)

Although I prefer to name my subjects explicitly:

subject(:company) { Company.new }

it 'something' do
  expect(company).to be_kind_of(Company)
end

Upvotes: 1

Related Questions