Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid and_return with RSpec mocks #176

Closed
jessieay opened this issue May 22, 2014 · 8 comments
Closed

Avoid and_return with RSpec mocks #176

jessieay opened this issue May 22, 2014 · 8 comments

Comments

@jessieay
Copy link
Contributor

With RSpec mocks, the following:

receiver.stub(:message).and_return(:return_value)

is equivalent to:

receiver.stub(message: :return_value)

I prefer the second. Any objections/ideas?

@jessieay
Copy link
Contributor Author

cc @gylaz who first pointed this out to me

@mcmire
Copy link

mcmire commented May 22, 2014

I like to think one reason we use rspec-mocks is for its readability. and_return is certainly more to type, but it's more readable.

@gabebw
Copy link
Contributor

gabebw commented May 22, 2014

I prefer the second as well.

However, if we switch to #175, I think we'll have to use and_return, since I don't know how to use the second version with expect(thing).to receive(:meth) syntax.

@gabebw
Copy link
Contributor

gabebw commented May 22, 2014

Apparently you can do this:

allow(user).to receive_messages(title: 'Developer', name: 'Greg')

I vote for that if #175 is merged.

@mcmire
Copy link

mcmire commented May 22, 2014

Ooh, I quite like that.

@jferris
Copy link
Contributor

jferris commented Sep 11, 2014

@jessieay want to reopen this in pull request form?

@jferris jferris closed this as completed Sep 11, 2014
@gylaz
Copy link
Contributor

gylaz commented Sep 11, 2014

Considering stub is deprecated in new versions of RSpec in favor of allow, this is probably somewhat irrelevant.

@jessieay
Copy link
Contributor Author

@jferris @gylaz I think Joe was referring to allow(user).to receive_messages(title: 'Developer', name: 'Greg') as a style guideline for stubbing multiple methods. I like that and will open a PR for it shortly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants