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

Disable VCR in a scenario? VCR.turned_off seems doesn't work for me. #407

Closed
samnang opened this issue May 1, 2014 · 7 comments
Closed

Comments

@samnang
Copy link

samnang commented May 1, 2014

I tried to temporary disable VCR in a senario in my spec suit. But I can't get it to work. I have tried different options like VCR.turned_off, VCR::Configuration#ignore_hosts, and VCR::Configuration#ignore_request, but none of those work for me. When I tried to comment vcr in my Gemfile, it works just fine. I'm using vcr-2.9.0 with webmock-1.16.1.

require 'spec_helper'

describe S3PhotoWorker do
  let(:remote_file_url) { "http://s3.amazon.com/files/photo.jpg" }

  it 'pulls image from s3 for processing' do
    VCR.turned_off do
      stub_request(:get, remote_file_url)
        .to_return(body: fixture_file('post_photo.jpg'),
                   headers: { 'Content-Type' => 'image/jpg' })

      post = create(:post, unprocessed_photo_url: remote_file_url)

      S3PhotoWorker.new.perform(post, remote_file_url)

      expect(post.reload.photo.url).not_to be_blank
    end
  end
end

Here is the error message:

Failures:

  1) S3PhotoWorker pulls image from s3 for processing
     Failure/Error: S3PhotoWorker.new.perform(post, remote_file_url)
     ActiveRecord::RecordInvalid:
       Validation failed: Photo could not download file: VCR::Response initialized with an invalid body: #<Rack::Test::UploadedFile:0x007fb03cdd4b60 @content_type="text/plain", @original_filename="post_photo.jpg", @tempfile=#<Tempfile:/var/folders/nv/csqxfl2d7xsfmsqwn2zpp72c0000gn/T/post_photo.jpg20140501-20008-17xypia>>.
     # ./app/workers/s3_photo_worker.rb:7:in `perform'
     # ./spec/workers/s3_photo_worker_spec.rb:16:in `block (3 levels) in <top (required)>'
     # ./spec/workers/s3_photo_worker_spec.rb:7:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'

Finished in 0.4201 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/workers/s3_photo_worker_spec.rb:6 # S3PhotoWorker pulls image from s3 for processing
@TigerWolf
Copy link

Some things to try:

WebMock.allow_net_connect!
and
VCR.turn_off! (without a block)
and
VCR.eject_cassette (as well as turn off)

This may help narrow down the problem, source:
https://relishapp.com/vcr/vcr/v/2-5-0/docs/cassettes/error-for-http-request-made-when-no-cassette-is-in-use

@myronmarston
Copy link
Member

VCR.turned_off doesn't disable VCR's adapter hooks -- it simply acts like VCR.configuration.ignore_request { true } -- essentially leveraging the ignore mechanism to ignore all requests. However, since the ignoring API provides a VCR::Request to the block, it still hooks into the adapters and constructs a VCR::Request and VCR::Response from the request and response.

Both VCR::Request and VCR::Response expect the body to be a string, and can't operate on another type of object (since ultimately, the body of an HTTP request or response is just a sequence of bytes). The problem is that you are providing a Rack::Test::UploadedFile for the response body, not a string. Try changing .to_return(body: fixture_file('post_photo.jpg'), to .to_return(body: fixture_file('post_photo.jpg').tempfile.read,.

@bblimke, is WebMock meant to support to_return(body: <a Rack::Test::UploadedFile>)? If so, it should convert it to a string before contructing the objects it passes to VCR. Or, if it doesn't support that, maybe it could fail fast and instruct users to pass a string instead.

@samnang
Copy link
Author

samnang commented May 1, 2014

thank @myronmarston, you are right it works when I changed to to_return(body: fixture_file('post_photo.jpg').read, to set string to body.

@samnang samnang closed this as completed May 1, 2014
@bblimke
Copy link
Contributor

bblimke commented May 2, 2014

@myronmarston it doesn't support it. yes, probably it's a good idea to provide an error message if invalid value is provided.

@myronmarston
Copy link
Member

@myronmarston it doesn't support it. yes, probably it's a good idea to provide an error message if invalid value is provided.

Would you like me to open up a webmock issue about this?

@bblimke
Copy link
Contributor

bblimke commented May 3, 2014

@myronmarston sure, please do :)

@chrishough
Copy link

could this be related to #950 ?

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