Mocking HTTP requests #18

Open
andreareginato opened this Issue Oct 3, 2012 · 23 comments

Projects

None yet

9 participants

@andreareginato
Collaborator

Write your thoughts about the "mocking HTTP requests " best practice.

@mattvanhorn

VCR is a nice library for both mocking the request, and occasionally sanity-checking the API.
Docs are here

@marnen
marnen commented Oct 3, 2012

I wrote a brief presentation on testing with Webmock and VCR; it's at http://marnen.github.com/webmock-presentation/webmock.html . Feel free to include a link if it's useful.

@pawel2105

Any suggestions for Rails 2 apps?

@marnen
marnen commented Oct 4, 2012

@pawel2105 I've used Webmock and FakeWeb with Rails 2.

@dchelimsky

I'd recommend changing the name of the topic to "stubbing HTTP requests" since what you're describing is stubbing (defining controlled responses to messages), not mocking (expecting that messages are received and failing when they are not).

@marnen
marnen commented Oct 6, 2012

@dchelimsky The terminology is kind of unfortunate. "Mock object" really should have meant any test double, IMHO, but the term got appropriated for one particular type. You're right that a lot of HTTP mocking is more like stubbing.

However...as I think about it, I realize that WebMock may be more like a true mock. If you use record_mode :once (which I always do), then the test fails if a request is made that's not in the cassette. At that point the WebMock cassette really starts to behave like a self-verifying mock object in the Martin Fowler sense, or so it seems to me.

None of this really has any bearing on the test practice itself. But the argument is interesting. :)

@andreareginato
Collaborator

@dchelimsky I agree with the change on the strict meaning of stub and mock.
I'll also add the VCR link about site and presentation. Thanks for sharing.

@makaroni4

@andreareginato fix topic name: #43

@andreareginato
Collaborator

Thanks a lot. I'll take a day off to merge all needed changes, so keep little patience.

@makaroni4

You are welcome)

@andreareginato what do you think of http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax? I think we should add this to betterspecs.

@pepe
pepe commented Nov 26, 2012

@makaroni4 👍 for expect

@andreareginato
Collaborator

The pull request is on master. I've updated Mocking to Stubbing.
@makaroni4 As far as the new expectation syntax will be used more and more I would love a pull request for it.

Any correction and comment to the updated guideline is appreciated.

@passion8
passion8 commented Apr 7, 2013

I think you should also change the description from "Sometimes you need to access external services. In these cases you can't rely on the real service but you should mock it with solutions like webmock." to "Sometimes you need to access external services. In these cases you can't rely on the real service but you should stub it with solutions like webmock."

@andreareginato
Collaborator

@Passionate, you are right. Send me a pull request and I'll merge the docs.

@passion8
passion8 commented Apr 8, 2013

@andreareginato , sent .

@onebree
Contributor
onebree commented Mar 16, 2015

This page needs to be updated. Webmock is no longer maintained for RSpec 3, as per their README. As a suggestion, how about using RSpec's Mocks library? I am not familiar with it myself enough to write example code for it, though

@marnen
marnen commented Mar 16, 2015

@onebree Apparently Webmock does work with RSpec 3: see the resolved issue at bblimke/webmock#396. I haven't tried it myself yet, though.

I'd be surprised if it weren't compatible with RSpec 3, given that that's what Webmock's own tests use. :)

@onebree
Contributor
onebree commented Mar 16, 2015

I am using RSpec 3.1. At this point, I am just going to use the RSpec Mocks library that RSpec provides. I am also not comfortable using a gem that seems to not be maintained anymore... Given their badges that say failed build and out-of-date dependencies

@andreareginato
Collaborator

Right now I'm working on different project and would be great if you could
send us a pull request solving the issue, where we can discuss the use the
RSpec mock library together with the changed code examples.

Thanks a lot Hunter.

On Mon, Mar 16, 2015 at 6:12 PM, Hunter Stevens notifications@github.com
wrote:

I am using RSpec 3.1. At this point, I am just going to use the RSpec
Mocks library that RSpec provides. I am also not comfortable using a gem
that seems to not be maintained anymore... Given their badges that say
failed build and out-of-date dependencies


Reply to this email directly or view it on GitHub
#18 (comment)
.

Andrea Reginato
Lelylan | reThink your house
http://lelylan.com

@marnen
marnen commented Mar 16, 2015

@onebree Where do you get the impression that Webmock isn't maintained anymore? Last release (v.1.20.4) was November 2014, about 4 months ago, and last commit to master was 22 days ago.

Webmock does a lot of good stuff. Don't reinvent it if you don't have to.

@onebree
Contributor
onebree commented Mar 16, 2015

On its README, it says it is only supporting up to RSpec 2.x, and its badges stated failed builds and out-of-date dependencies.

Although I did contact someone on the Google Group -- they said they are updating the README as it does support RSpec 3.

@marnen
marnen commented Mar 16, 2015

FWIW, I've never had any issues with Webmock; I suspect the failed build is more likely to mean that they never set up Travis properly. :)

@onebree
Contributor
onebree commented Mar 16, 2015

As I told the owner in the google group forum:

Thank you... My spec or related MVC files may not be up to date fully with Rails 4, causing something to break during the tests.

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