Expect vs Should syntax #83

Closed
maurogeorge opened this Issue Aug 7, 2013 · 11 comments

Projects

None yet

8 participants

@maurogeorge

In the following text Myron says to use this on new projects.

You guys think this is a good section to better specs? I personaly have use this on new projects with configuration to use only new syntax.

@gabetax
gabetax commented Aug 8, 2013

+1

expect syntax is mature. Given that should is deprecated in 3, and removed from 4, I would recommend switching all the documentation to use expect by default, and then adding an "expect vs should" section. It's a waste for brand new rspec users to learn a syntax that's going away when the new syntax is available today.

@jandudulski

👍

@jguice
jguice commented Oct 1, 2013

If expect is to be used instead of should, can the other examples be updated to reflect that? :)

e.g. the "Use contexts" guideline...

@maurogeorge

@jguice all examples are now using the expect syntax.

The context guideline, and all one-liner syntax(with implicit subject) still use the should syntax. More info about it here on a answer of Stack Overflow by Myron

@jguice
jguice commented Oct 2, 2013

Ah! I think what is confusing in the contexts example is that, when going from BAD to GOOD, not only is the context added but the "it" is changed to a one-liner which also makes the "expect" become a "should". It seems like the only changes from a BAD example to a GOOD example should be directly related to the particular guideline being illustrated for maximum clarity ;)

Also adding a note to the Expect vs. Should guideline about the one-line format still (acceptably) using should might be helpful.

I found a minor typo on Expect vs. Should too: "Configure the Rspec to only accept the new syntax on new projects, to avoid have the 2 syntax all over the place." (the "have" should be a "having").

Let me know if I should create a pull request or new issue for these :)

@maurogeorge

@jguice I like the idea of have a note on Expect vs. Should section. Something like:

In one line expects or with implicit subject we still use the should syntax, since is no change here on new syntax

And add the link to Myron answer on Stack Overflow.

@andreareginato
Collaborator

If you find the time guys, would be perfect to add this little note.

@yujinakayama

By the way, for people who are willing to maintain existing should syntax specs, I'd like to recommend converting them to expect syntax by using Transpec.

@maurogeorge

Done in here #91

@voltechs

typo.

s/since is no change/since there is no change/

@mltsy
mltsy commented Nov 16, 2015

I agree that expect is the correct syntax to use for block expectations, but for one-liners, it's actually still perfectly okay to use should, as it's a method from a different class entirely (it's not the same should) and it sounds and looks much better than it { is_expected.to ... }. The only reason the other should was removed was because it required monkey-patching Object. That is not the case with this should, and it has not been removed as of RSpec 3.4, and doesn't look like it will be.

I think both syntaxes for one-liners should be on the "GOOD" list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment