ruby on rails 3 - undefined method `has_content?' for "HomePages":String

2.3k Views Asked by At

i'm new to ruby and i just start my first project. when i'm trying to run my first test throught rspec i failed.

this is my gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.12'
gem 'bootstrap-sass', '2.3.0.1'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'jquery-rails', '2.2.1'
gem 'mysql2'
#gem 'execjs'
gem 'therubyracer', :platforms => :ruby

# Dev and test gems
group :development, :test do
  gem 'rspec-rails', '2.13.0'
  gem 'guard-rspec', '2.4.1'
  gem 'guard-spork', '1.4.2'
  gem 'spork', '0.9.2'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.6'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.3.0'
end

#only test gems
group :test do
  gem 'capybara', '2.0.2'
  gem 'factory_girl_rails', '4.2.1'
  gem 'cucumber-rails', '1.3.0', :require => false
  gem 'database_cleaner', '0.9.1'
  # gem 'launchy', '2.1.0'
  # gem 'rb-fsevent', '0.9.1', :require => false
  # gem 'growl', '1.0.3'
end

group :production do
  gem 'pg', '0.12.2'
end

and here is my home_pages_spec.rb

require 'spec_helper'

describe "HomePages" do
  before { get 'home_page/home'}
  it { should have_content("Home")}
end

when i run

bundle exec rspec spec/requests/home_pages_spec.rb

i get

Failures:

  1) HomePages 
     Failure/Error: it { should have_content("Home")}
     NoMethodError:
       undefined method `has_content?' for "HomePages":String
     # ./spec/requests/home_pages_spec.rb:5:in `block (2 levels) in <top (required)>'

please help me, i search a little and saw similar problem but not as quite.. plus they were kinda old and what used to be bug isnt now.

plus if you can explain to me why i cant use the visit command (also undefined method `visit' for RSpec::Core::ExampleGroup::Nested_1...) and have to use the get command it will be great

3

There are 3 best solutions below

0
On BEST ANSWER

The method get is from rspec-rails, and it allows us to do this:

specify { response.should have_content("Foo") }

The method visit is from capybara, and it allows us to do this:

specify { page.should have_content("Foo") }

These methods used to be available to all specs in spec/requests, but as of Capybara 2.0, these have been separated. We now place Capybara integration tests in spec/features.

Please see this blog post for why this was done.

1
On

I believe you are looking for rspec's have_content() http://rubydoc.info/github/jnicklas/capybara/master/Capybara/RSpecMatchers

change the line: it { should have_contact("Home")}

to: it { should have_content("Home")}

hope this helps

0
On

if you use it instead of expect(page).to, you have to declare what is the it:

describe "HomePages" do
  before { get 'home_page/home'}
  subject { page }
  it { should have_content("Home")}
end

or you can simply do:

describe "HomePages" do
  before { get 'home_page/home'}
  expect(page).to have_content('Home')
end