Using faraday_middleware Mashify with VCR

482 views
Skip to first unread message

Adam McDonald

unread,
May 28, 2011, 1:03:07 AM5/28/11
to vcr-...@googlegroups.com

First off, this gem is amazing and I'm looking forward to using it more in the future!

Having some issues with the recording when I have Faraday set to use Faraday::Response::Mashify via the faraday_middleware gem (https://github.com/pengwynn/faraday_middleware).

NoMethodError:
  undefined method `items' for #<Hash:0x00000100d114a0>

This is the case because I'm calling response.body.items (dot notation provided via Mashify). That object should be a Hashie type, not Hash. So, I was curious how to ensure VCR uses the same middleware.

A single connection object is used throughout various classes and is setup like so:

        @connection ||= Faraday::Connection.new(:url => connection_url, :headers => connection_headers) do |builder|
          builder.adapter Faraday.default_adapter
          builder.use Faraday::Response::ParseJson
          builder.use Faraday::Response::Mashify
        end

Myron Marston

unread,
May 28, 2011, 1:52:37 AM5/28/11
to VCR Rubygem
Hmm...this sounds like a bug in VCR. What VCR configuration are you
using? Can you come up with a reproducible example that I can use to
troubleshoot?

I wonder also if it would make a difference to put the adapter last
(instead of first) in the Faraday connection block. Given that
faraday builds a middleware stack (much like rack does) and that you
put your adapter before the response middlewares, it's conceivable
that it could be short-circuiting mashify based on the placement in
the faraday stack.

Myron

Adam McDonald

unread,
May 28, 2011, 2:02:03 PM5/28/11
to vcr-...@googlegroups.com
Myron, thanks for the quick reply. I tried swapping the ordering of when the adapter was defined for the connection but it didn't seem to have an effect. I've attached a slimmed down version of the gem I'm working on for a reproducible example. Just run $ rspec spec/project_spec.rb, and you'll see the error I've outlined above.
vcr_faraday.zip

Myron Marston

unread,
May 28, 2011, 11:49:45 PM5/28/11
to VCR Rubygem
>  vcr_faraday.zip
> 12KViewDownload
>  vcr_faraday.zip
> 12KViewDownload

Thanks for the example code...that was really, really helpful. In the
future, you might consider pushing a repo to github so that I can just
fork it and hack away at it. This worked great though :).

I spent some time with it, and discovered that the problem wasn't VCR
at all; it was the order of your middleware. When I removed VCR
entirely from the project, the sam error (undefined "items" for Hash)
remained. Reordering the middleware fixed the problem [1].

That said, I don't think you have VCR configured in this project in a
way that will work well. Specific changes I would recommend:

* Don't require VCR in your lib files [2] (unless you are building a
gem that specifically works with and extends VCR). VCR is meant for
your test environment only.
* You've configured VCR to stub with faraday, but to do so, you also
need to use the faraday middleware shipped with VCR [3]. It doesn't
look like you've configured Faraday to use it. Alternately, you can
use VCR to stub with WebMock, Typhoeus, Fakeweb or Excon, and that'll
stub the underlying HTTP adapter used by Faraday.

Myron

[1] https://github.com/myronmarston/raid5_vcr_issue/commit/42033c1d02aeaab70a7dcf6a136e9566311df6a3
[2] https://github.com/myronmarston/raid5_vcr_issue/blob/master/lib/agilezen.rb#L4
[3] http://relishapp.com/myronmarston/vcr/v/1-10-0/dir/middleware/faraday-middleware

Adam McDonald

unread,
May 31, 2011, 2:20:17 PM5/31/11
to vcr-...@googlegroups.com
Great suggestions! I've taken these three items into account and switched to stubbing with WebMock. My spec now passes but doesn't seem to create a cassette. As you can see, I have `use_vcr_cassette` within the `describe` block.

Adam McDonald

unread,
May 31, 2011, 2:31:37 PM5/31/11
to vcr-...@googlegroups.com
Ignore the missing cassette issue :) I had a wrong path, doh! Everything is working great, thanks for the help!
Reply all
Reply to author
Forward
0 new messages