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

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

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

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.

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

Related

undefined method `page' for # Kaminari::PaginatableArray

I have been using Kaminari since a while. I use to paginate generic arrays using the following line.
Kaminari.paginate_array([1,2,3]).page(1).per(1)
That use to work before but I noticed that since yesterday this show the error
undefined method `page' for #<Kaminari::PaginatableArray:0x007f8e5d10fde0>
However calling page on ActiveRecord relation is working fine. something like this is working
User.page(1).per(1)
My Gem file
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '3.2.8'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'mysql2'
gem 'pg'
# Ok lets try out the heroku business
gem 'heroku'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-fileupload-rails'
gem 'jquery-rails'
gem 'jquery-ui-rails'
# Twitter Boostrap and Upgrade dependencies
gem 'therubyracer'
gem 'less-rails'
gem 'twitter-bootstrap-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
gem 'unicorn'
# Deploy with Capistrano
gem 'capistrano'
gem 'cap_bootstrap', github: 'benrs44/cap_bootstrap'
gem 'awesome_print'
group :test, :development do
gem 'rspec-rails', '>= 2.0.1'
gem "capybara-webkit"
gem 'capybara'
gem 'launchy'
gem 'selenium-webdriver'
gem 'shoulda-matchers'
gem 'ci_reporter'
gem "factory_girl_rails", "~> 4.0"
# DRb server RSpec
gem "spork-rails"
# To use debugger
gem 'debugger'
end
group :test do
gem "guard-rspec"
gem 'rb-fsevent', '~> 0.9.1'
gem 'database_cleaner'
gem 'fuubar'
end
#Image Uploader
gem 'carrierwave'
#Image Processor
gem "rmagick"
#Video Processor
gem 'video_info'
gem 'hpricot'
#Authenitcation
gem 'omniauth-facebook', '1.4.0'
#Messaging
gem 'mailboxer', github: 'rralston/mailboxer'
gem 'haml-rails'
gem 'simple_form'
#Gelocation
gem 'geocoder'
#Search
gem 'ransack'
# For easily making nouns possessive
gem 'possessive'
#Pagination
gem "kaminari"
gem 'will_paginate', '~> 3.0'
#Web App Monitoring
gem 'newrelic_rpm'
#FB Style Newsfeeds
gem 'public_activity'
# Heroku CDN Link to AWS
gem "asset_sync"
# Carrier Wave link to AWS S#
gem 'fog', '~> 1.12.1'
gem 'cancan'
# client side validtions
gem 'client_side_validations'
# tagging
gem 'acts-as-taggable-on'
# remote file uploads
gem "remotipart", "~> 1.2.1"
# social sharing
gem 'social-share-button'
gem 'devise', "~> 2.2.3"
gem 'omniauth'
gem 'devise-async'
running bundle show bundle show kaminari gives /Users/***/.rvm/gems/ruby-1.9.3-p448/gems/kaminari-0.14.1
Turned out to be that my colleague has added a configuration file for Kaminari and changed this page function with this line.
config.page_method_name = :kaminari_page
So instead of using Kaminari.paginate_array([1,2,3]).page(1).per(1) I should now use
Kaminari.paginate_array([1,2,3]).kaminari_page(1).per(1)

Rails 3.2, RSpec, Factory Girl : NameError: uninitialized constant Factory

Ive been following this introductory to Rails testing and Ive run into an issue I cant seem to find the solution to. Im very familiar with Rails but this is my first foray into testing.
Anyhow, I have a very basic model test, not even fully implemented and when I try and run rspec spec/models/admin_spec.rb. I get the following error in the Admin has a valid factory line (full code below)
Admin has a valid factory
Failure/Error: Factory.create(:admin).should be_valid
NameError:
uninitialized constant Factory
# ./spec/models/admin_spec.rb:6:in `block (2 levels) in <top (required)>'
I assume FactoryGirl isnt being loaded for some reason but I was under the impression it should be automatically loaded. Below is the full code from my Gemfile, /spec/models/admin_spec.rb and /spec/factories/admins.rb
Thanks very much for your help
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.2'
gem 'mysql2'
gem 'jquery-rails'
gem 'haml'
gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'bootstrap-sass', '~> 2.0.2'
gem 'capistrano'
gem 'json'
gem "paperclip", '~>3.0'
gem 'airbrake'
gem 'acts_as_list'
gem 'nested_form', :git => 'https://github.com/ryanb/nested_form.git'
gem 'bootstrap-wysihtml5-rails'
gem 'will_paginate', '~> 3.0'
gem 'bootstrap-will_paginate'
gem 'thinking-sphinx', '2.0.10'
gem 'sass-rails', '~> 3.1'
gem 'coffee-rails'
gem 'uglifier'
# gem 'compass'
group :development do
gem 'awesome_print'
gem 'wirble'
end
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
end
group :production do
gem 'execjs'
gem 'therubyracer'
end
group :test do
# Pretty printed test output
gem 'turn', :require => false
gem 'faker'
gem 'capybara'
gem 'guard-rspec'
gem 'launchy'
end
/spec/factories/admin.rb
require 'faker'
FactoryGirl.define do
factory :admin do |f|
f.name Faker::Name.name
f.email Faker::Internet.email
end
end
/spec/models/admin_spec.rb
require 'spec_helper'
describe Admin do
it "has a valid factory" do
Factory.create(:admin).should be_valid
end
it "is invalid without a name"
it "is invalid without an email"
end
It should be FactoryGirl.create instead. Apparently Factory was deprecated and now has been removed, look at the comments in the link you provided :)
In fact in your spec_helper.rb under Rspec.configure do...end you can add
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
This will save you the trouble of the prefixing FactoryGirl. before :build and :create altogether:
require 'spec_helper'
describe Admin do
it "has a valid factory" do
create(:admin).should be_valid
end
it "is invalid without a name"
it "is invalid without an email"
end
Refer: FactoryGirl Documentation
This isn't an answer to your question, but I noticed there is an obscure error in your use of Faker with FactoryGirl. f.name and f.email will be the same for every FactoryGirl.create or FactoryGirl.build.
f.name Faker::Name.name
f.email Faker::Internet.email
Add curly braces around the Faker calls so that each reference to a Factory will generate random Faker data.
f.name { Faker::Name.name }
f.email { Faker::Internet.email }
Also, be sure that you are including the require statement in your spec_helper.rb file.
require 'factory_girl_rails'

Trouble with Guard init command

I'm trying to setup Guard using this tutorial, however when I run bundle exec guard init or bundle exec guard init rspec
I'm on OSX, running RVM 1.14.1 and Rails 3.2.5.
I get the following errors:
[myapplication]$ bundle exec guard init rspec
/Users/ash/.rvm/gems/ruby-1.9.2-p290#myapplication/gems/guard-1.1.0/lib/guard/cli.rb:145:in `init': undefined method `create_guardfile' for Guard:Module (NoMethodError)
from /Users/ash/.rvm/gems/ruby-1.9.2-p290#myapplication/gems/thor-0.15.2/lib/thor/task.rb:27:in `run'
from /Users/ash/.rvm/gems/ruby-1.9.2-p290#myapplication/gems/thor-0.15.2/lib/thor/invocation.rb:120:in `invoke_task'
from /Users/ash/.rvm/gems/ruby-1.9.2-p290#myapplication/gems/thor-0.15.2/lib/thor.rb:275:in `dispatch'
from /Users/ash/.rvm/gems/ruby-1.9.2-p290#myapplication/gems/thor-0.15.2/lib/thor/base.rb:408:in `start'
from /Users/ash/.rvm/gems/ruby-1.9.2-p290#myapplication/gems/guard-1.1.0/bin/guard:6:in `<top (required)>'
from /Users/ash/.rvm/gems/ruby-1.9.2-p290#myapplication/bin/guard:19:in `load'
from /Users/ash/.rvm/gems/ruby-1.9.2-p290#myapplication/bin/guard:19:in `<main>'
RSpec runs fine:
[myapplication]$ bundle exec rspec
F
Failures:
1) StaticPages GET /static_pages works! (now write some real specs)
Failure/Error: get static_pages_index_path
NameError:
undefined local variable or method `static_pages_index_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fd6d176a020>
# ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.00887 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:5 # StaticPages GET /static_pages works! (now write some real specs)
Also my gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.5'
group :development, :test do
gem 'rspec-rails'
gem 'guard-rspec'
gem 'growl'
gem 'rb-fsevent'
gem 'spork-rails'
gem 'guard-spork'
end
gem 'sqlite3'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
I just had the same problem in Ubuntu Server 11.10. In my case there was some problem with the versions of the gems. I changed the guard gem version in the Gemfile from 1.1.0 to 0.10.0 and it worked. Here is my Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'pg', '0.12.2'
group :development, :test do
#gem 'sqlite3', '1.3.5'
gem 'pg', '0.12.2'
gem 'guard','0.10.0'
gem 'rspec-rails', '2.9.0'
gem 'guard-rspec', '0.5.5'
gem 'guard-bundler'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test do
gem 'capybara', '1.1.2'
gem 'rb-inotify', '0.8.8'
gem 'libnotify', '0.5.9'
end
group :production do
gem 'pg', '0.12.2'
end
I've reported this issue and it has been fixed:
https://github.com/guard/guard/issues/283#issuecomment-6078721

Rails 3 Coffee Handler Missing

Im using Ruby 1.9.3p0, Rails 3.1.2 with Apache and Passenger in Production. When I try to make a remote function, it throws me this error:
ActionView::MissingTemplate (Missing template video/loadVideo, application/loadVideo with
{:handlers=>[:erb, :builder], :formats=>[:js, "application/ecmascript", "application/x-
ecmascript", :html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml,
:multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]}. Searched in
As you can see, there is no handler for :coffee, that's the reason why is not processing the file video/loadVideo.js.coffee; I run the command rake assets:precompile but nothing changes.
The Gemfile
source 'http://rubygems.org'
gem 'rails', '3.1.2'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'will_paginate', '~> 3.0'
gem "bcrypt-ruby", :require => "bcrypt"
gem 'mysql'
#Problems in Debian, the error was:
#Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs #for a list of available runtimes. (ExecJS::RuntimeUnavailable)
#Solution: http://stackoverflow.com/questions/6282307/rails-3-1-execjs-and-could-not-find-a-javascript-runtime
gem 'execjs'
gem 'therubyracer'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.1.5.rc.2'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :test do
# Pretty printed test output
gem 'turn', '0.8.2', :require => false
end
Change your Gemfile (move coffee-rails gem outside the assets group) or change your application.rb settings - for instance
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
# Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
Bundler.require(:default, :assets, Rails.env)
end
In development environment application by default loads all of gems from :default, :development, :test, :assets groups. Coffee handler comes from coffee-rails gem, so you have to load it.

test-unit gem displays test stat for every rake run

I using test-unit gem with a Rails 3.1 project, and I have a small issue. If I start rake tasks, I always see a test result, with zeros.
My Gemfile is:
source :rubygems
gem 'rails', '3.1.2'
gem 'sqlite3'
gem 'json_pure'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
# gem 'sass-rails', '~> 3.1.5.rc.2'
# gem 'coffee-rails', '~> 3.1.1'
gem 'therubyracer'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# Rails i18n
gem 'rails-i18n', :git => 'git://github.com/hron84/rails-i18n.git'
# Paginator
gem 'will_paginate'
# To use ActiveModel has_secure_password
gem 'bcrypt-ruby', '~> 3.0.0'
# Use unicorn as the web server
gem 'thin'
# Deploy with Capistrano
gem 'capistrano'
# To use debugger
# gem 'ruby-debug'
group :development do
gem 'rails3-generators'
gem 'nifty-generators'
end
group :test do
gem 'rspec'
gem 'rspec-rails'
gem 'ci_reporter'
gem 'factory_girl'
unless defined? Rubinius
gem 'rcov'
gem 'rcov_rails'
gem 'test-unit'
end
gem 'ruby-prof'
gem 'faker'
end
# Application-related stuffs
gem 'authlogic'
# Slug helper
gem 'slug'
# vim: ft=Gemfile
Is there a way to avoid it? What I do wrong?
Check your RAILS_ENV. Make sure it's set to "development", not "test". If that's not the culprit, try removing gems (particularly test/spec-related ones) one by one until the problem goes away. Then you'll know which gem is giving you the problem.