What is this notation in rspec: it {is_expected_to ..}

I am reading the 'Better specs' page and in one of the examples it says:

context 'when logged in' do
  it { is_expected.to respond_with 200 }
end
context 'when logged out' do
  it { is_expected.to respond_with 401 }
end

      

And I don't get it. I used to do:

context 'when logged out' do
  it 'responds with a 401' do
    expect(response).to eq(401)
  end
end

      

What is this syntax?

+3


source to share


2 answers


It has a lot to do with Rspec 3.XX. It's guided by one line syntax as described here



RSpec supports a one-line syntax for setting expectation on a subject. RSpec will provide examples of the doc string generated from the mapping used in the example. This is designed to avoid duplication in situations where the document string and match used in this example accurately reflect each other. Overused, it can produce documentation output that is not readable or conducive to understanding of the object you are describing.

This happens in two ways:

is_expected is simply defined as expected (subject) and is meant to be when you use rspec waits with its new expectation syntax.

+4


source


it { is_expected.to respond_with 200 }

      



which is more readable. Why did you add a description if you can read it from the test. Your code should be simple, smart and readable at the same time ... but if you really want, you can even add a novel ... up to you :)

+2


source







All Articles