3

Please can someone explain to me how I should go about deciding whether to use expect or should syntax for my rspec examples.

From my understanding we should no longer be using the 'should' syntax.

The replacement is to use expect().to or is_expected.to

Is this correct?

On some really good references, it seems to be common practice to still use should. And these references are being updated consistently.

Any help in the correct direction would be appreciated.

References (+ others, but excluded)
https://github.com/thoughtbot/shoulda-matchers
https://www.railstutorial.org/book

  • Use whatever is best to read in the particular case. Readability is very important for specs. – back2dos Sep 11 '14 at 10:49
  • @back2dos I don't think you read the question.... – gbjbaanb Sep 11 '14 at 10:58
  • In 2019: 1. bundle open shoulda-matchers 2. checkout the source code 3. judge it your self. and I agree with @back2dos Readability is very important. shoulda-machers help a lot with that. – Khalil Gharbaoui Oct 26 '19 at 12:15
6

Yes you should use expect().to

.should is the original format but had some issues in certain circumstances

expect().to overcomes these issues

I have worked on several large github open source projects that have made the change over the past couple of years.

You may also find https://stackoverflow.com/a/19570645/631619 helpful

| improve this answer | |
  • So would you say that companies like thoughtbot have just not updated their documentation for all of their repos - e.g. github.com/thoughtbot/shoulda-matchers Would really like to stick to best practices and they really seem to know what they are doing. Just wanted to be thorough. Thanks. – Ryan-Neal Mes Sep 11 '14 at 11:42
  • 2
    I've taken courses at Thoughtbot and my instructors had made and recommended the switch. – Michael Durrant Sep 11 '14 at 12:17
  • Is the is_expected.to syntax excepted? I find my specs to be super clean using is_expected and shoulda matchers e.g. it { is_expected.to validate_uniqueness_of(:start_date).scoped_to(:product_id) } – Ryan-Neal Mes Sep 11 '14 at 13:21
  • I think that's the one format that didn't transition well (no short format) and we all miss it greatly. – Michael Durrant Oct 14 '14 at 19:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.