`Zlib::GzipFile::Error` when using `vcr` with rspec - ruby-on-rails

I'm doing Railscast #291 Testing with VCR (Pro).
I want to use rspec with vcr. The tests without vcr pass with this code.
# spec/requests/zip_code_lookup_spec.rb
require "spec_helper"
describe "ZipCodeLookup" do
it "show Beverly Hills given 90210" do
visit root_path
fill_in "zip_code", with: "90210"
click_on "Lookup"
page.should have_content("Beverly Hills")
end
end
And as the tutorial I've put code in a VCR.use_cassette like this:
require "spec_helper"
describe "ZipCodeLookup" do
it "show Beverly Hills given 90210" do
VCR.use_cassette "zip_code/90210" do
visit root_path
fill_in "zip_code", with: "90210"
click_on "Lookup"
page.should have_content("Beverly Hills")
end
end
end
And created this file:
# spec/support/vcr.rb
VCR.configure do |c|
c.cassette_library_dir = Rails.root.join("spec", "vcr")
c.stub_with :fakeweb
end
According to the tutorial the rspec test should pass with this, but it fails with this error:
1) ZipCodeLookup show Beverly Hills given 90210
Failure/Error: click_on "Lookup"
Zlib::GzipFile::Error:
not in gzip format
# ./app/models/zip_code.rb:6:in `initialize'
# ./app/controllers/zip_code_lookup_controller.rb:3:in `new'
# ./app/controllers/zip_code_lookup_controller.rb:3:in `index'
# ./spec/requests/zip_code_lookup_spec.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/requests/zip_code_lookup_spec.rb:5:in `block (2 levels) in <top (required)>'
I have no idea why gzip appears here and not in the rest of the rails project.
How can I solve this problem?

Just had the same issue: the fakeweb gem is now deprecated in VCR, use webmock instead
in your Gemfile, replace
gem 'fakeweb'
by
gem 'webmock'
re-bundle, and you should be sorted

Related

I keep getting this error " NoMethodError:undefined method `visit' for #<RSpec::ExampleGroups::" in rspec and rails 4.1

I'm working on this http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/ and I keep getting this error.
I'm using rails 4.1
terminal
Failures:
1) Tasks GET /tasks display some task
Failure/Error: visit tasks_path
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::Tasks::GETTasks:0x007fcfbd633758>
# /Users/estebangallego/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
# /Users/estebangallego/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/testing/integration.rb:396:in `method_missing'
# ./spec/features/task_spec.rb:7:in `block (3 levels) in <top (required)>'
2) Tasks GET /tasks creates a new task
Failure/Error: visit "/"
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::Tasks::GETTasks:0x007fcfbe572ea8>
# /Users/estebangallego/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
# /Users/estebangallego/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/testing/integration.rb:396:in `method_missing'
# ./spec/features/task_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 0.01248 seconds (files took 1.69 seconds to load)
4 examples, 2 failures, 2 pending
Failed examples:
rspec ./spec/features/task_spec.rb:5 # Tasks GET /tasks display some task
rspec ./spec/features/task_spec.rb:12 # Tasks GET /tasks creates a new task
I tried "features/task_spect.rb" and "requests/tasks_spect.rb"
require "rails_helper"
RSpec.describe "Tasks", type: :request do
describe "GET /tasks" do
it "display some task" do
#task = Task.create :task => "go to bed"
visit tasks_path
page.should have_content "go to bed"
end
it "creates a new task" do
visit "/"
fill_in "Task", :with => "go to work"
click_button "Create Task"
current_path.should == root_path
page.should have_content "go to work"
save_and_open_page
end
end
end
gems
group :development, :test do
gem 'rspec-rails'
gem 'capybara'
end
new rspec.rb
require 'rails_helper'
RSpec.describe "Tasks", type: :request do
config.include Capybara::DSL
describe "GET /tasks" do
it "display some task" do
#task = Task.create :task => 'go to bed'
visit root_path
page.should have_content 'go to bed'
end
it "creates a new task" do
visit '/'
fill_in 'Task', :with => 'go to work'
click_button 'Create Task'
current_path.should == task_path
page.should have_content 'go to work'
save_and_open_page
end
end
end
I have fixed the error and post it in my github app
https://github.com/rajcybage/tasks
please see the commit
https://github.com/rajcybage/tasks/commit/fa6fa54e60717f805e040f0eb767dc0e3ef4d434
run bundle exec rake spec:features
rajarshi#rajarshi-X200CA:~/tasks$ bundle exec rake spec:features
/home/rajarshi/.rbenv/versions/2.1.4/bin/ruby -I/home/rajarshi/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.3/lib:/home/rajarshi/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/rspec-support-3.2.2/lib /home/rajarshi/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.3/exe/rspec --pattern ./spec/features/\*\*/\*_spec.rb
Randomized with seed 14426
..
Deprecation Warnings:
Requiring `rspec/autorun` when running RSpec via the `rspec` command is deprecated. Called from /home/rajarshi/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'.
Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` with `config.expect_with(:rspec) { |c| c.syntax = :should }` instead. Called from /home/rajarshi/tasks/spec/features/task_spec.rb:18:in `block (3 levels) in <top (required)>'.
If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.
2 deprecation warnings total
Finished in 2.06 seconds (files took 6.03 seconds to load)
2 examples, 0 failures
Randomized with seed 14426
rajarshi#rajarshi-X200CA:~/tasks$

Capybara: undefined method 'visit' - Test is already in spec/features

I have just started with RSpec and Capybara but got stuck on my first test.
Here's me test code located in spec/features/pages_spec.rb :
require 'rails_helper'
RSpec.describe "Pages", :type => :request do
describe "About Page" do
it "should have the content 'About Us'" do
visit '/pages/about'
page.should have_content('About Us')
end
end
end
Running the test i get the following error :
01:06:59 - INFO - Running: spec
F
Failures:
1) Pages About Page should have the content 'About Us'
Failure/Error: visit '/pages/about'
NoMethodError:
undefined method 'visit' for #<RSpec::ExampleGroups::Pages::AboutPage:0x007f975afe7380>
# ./spec/features/pages_spec.rb:6:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:44:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:43:in `block (2 levels) in <top (required)>'
Finished in 0.02569 seconds (files took 1.65 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/pages_spec.rb:5 # Pages About Page should have the content 'About Us'
I've been searching about this for about an hour and everywhere I find the solution of moving the test code from spec/requests to spec/features.
I've also seen this here : http://www.rubydoc.info/gems/rspec-rails/file/Capybara.md which most of the solution suggests and it is not recommended to use.
# not recommended!
RSpec.configure do |c|
c.include Capybara::DSL, :file_path => "spec/requests"
end
I've no idea how to proceed. Your help will be appreciated.
Either take :type => :request out of the describe block (which overrides the spec type determined from the directory location) or change it to :type => :feature.

Capybara NoMethodError

I am currently reading Michael Hartl's Rails book, and am on chapter 3 where you begin to write tests. My problem is, I can't get the first test to work. From googling and reading several threads no stackoverflow, as well as the Capybara readme, I have made some changes from his code however I still cannot get the visit function to work. The relevant files are included below.
spec/spec_helper.rb
require 'capybara'
require 'capybara/rspec'
Rspec.configure do |config|
config.include Capybara::DSL
end
Capybara.configure do |config|
config.app = "Sample App"
end
features/static_pages_spec.rb
require 'spec_helper'
feature "Static pages" do
feature "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
And the relevant error message that is throwing me for a loop:
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `call' for "Sample App":String
#./spec/features/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Seeing the error I have tried removing config.app from spec_helper.rb, but that just leads to another error.
Failure/Error: visit '/static_pages/home'
ArgumentError:
rack-test requires a rack applicaiton, but none was given
#./spec/features/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Not sure what the reasoning is, but after doing more research to fix this issue in spec/features/static_pages_spec.rb I needed to change require 'spec_helper' to require 'rails_helper'.

Ruby on Rails TDD; Error while testing

I am trying to follow TDD on Rails Tutorial which is available online here
While testing first app, I got an error.
My spec.rb code is this:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
end
After running testing I got this error:
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for # <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa833e5c># ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
I will really appreciate your help.
The visit method is not part of RSpec - it's provided by capybara. Just add this to your Gemfile:
gem 'capybara'
Try add:
require 'rails_helper'
require 'spec_helper'
to your spec.rb
and:
require 'capybara'
RSpec.configure do |config|
config.include Capybara::DSL
....
to spec_helper.rb
and gem 'capybara', '2.2.0' to gemfile

Migrating from Webrat to Capybara...unsuccessfully

Hoping someone might see what I've overlooked...
I'm trying to get Capybara working in a small existing application...and I'm not having any luck.
Gemfile:
group :development, :test do
gem 'rspec-rails'
# gem 'webrat'
gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
end
...
Similar specs in two places are failing for different reasons. Not sure why?
spec/controllers/pages_controller_spec.rb:
require 'spec_helper'
describe PagesController do
describe "GET 'about'" do
it "should be successful" do
# get 'about' #worked w/ webrat
# response.should be_success #worked w/ webrat
visit pages_about_path
# page.should have_content('About Us')
page.html.should match(/About/i)
end
it "should have title" do
# get 'about' #webrat
# response.should have_selector("title", :content => "About Us") #webrat
visit pages_about_path
page.should have_selector("title")
end
end
end
Failures:
(may be pulling in some generic page as doctype in browser is "<!DOCTYPE html>")
1) PagesController GET 'about' should be successful
Failure/Error: page.html.should match(/About/i)
expected "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n\n" to match /About/i
# ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
2) PagesController GET 'about' should have the right title
Failure/Error: page.should have_selector("title")
expected css "title" to return something
# ./spec/controllers/pages_controller_spec.rb:20:in `block (3 levels) in <top (required)>'
spec/views/pages/about.html.haml_spec.rb:
require 'spec_helper'
describe "pages/about.html.haml" do
it "renders attributes in <p>" do
# render #webrat
# rendered.should match(/About/) #webrat
visit pages_about_path
page.should have_content("About Us")
end
it "should have the right heading" do
# render #webrat
# rendered.should have_selector("h2", :content => "About Us") #webrat
visit pages_about_path
page.should have_selector("h2")
end
end
Failures:
1) pages/about.html.haml renders attributes in <p>
Failure/Error: visit pages_about_path
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000101dc2970>
# ./spec/views/pages/about.html.haml_spec.rb:8:in `block (2 levels) in <top (required)>'
2) pages/about.html.haml should have the right heading
Failure/Error: visit pages_about_path
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x000001034b1d98>
# ./spec/views/pages/about.html.haml_spec.rb:16:in `block (2 levels) in <top (required)>'
Just had the same issue and found that capybara needs:
response.body.should have_selector("title")
webrat does not need the .body
also, make sure you are rendering views eg:.
describe PagesController do
render_views
You should include capybara DSL in your test file :
require 'spec_helper'
describe PagesController do
include Capybara::DSL
describe "GET 'about'" do
it "should be successful" do
...
This won't fix all your problems, but Instead of:
page.html.should match(/About/i)
try:
page.should match(/About/i)
(you might not even need page).
Also, check that you have Capybara set to use CSS queries in env.rb (it defaults to XPath).
Ensure that your spec_helper.rb file has the following at the top:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
I had a similar issue as yours (i.e., undefined method 'visit') and popping those lines solved the issue.

Resources