i'm sure this is answered somewhere on stackoverflow but for the life of me, i can't find why my situation is different than what is already written so here goes.
using rspec w/ capybara
1) report_cards#index must have 'Report Cards Index'
Failure/Error: visit '/'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_4:0x007fcc3aa33960>
# ./spec/views/report_cards_view_spec.rb:7:in `block (2 levels) in <top (required)>'
/spec/views/report_cards_view_spec.rb looks like
require 'spec_helper'
describe "report_cards#index" do
it "must have 'Report Cards Index'" do
visit '/'
page.should have_content("something")
end
end
top lines from spec_helper.rb looks like
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/rspec'
gemfile looks like
`source 'https://rubygems.org'`
gem 'rails', '3.2.7'
gem 'jquery-rails'
gem 'pg', '~> 0.14.0'
gem 'devise', '~> 2.1.2'
gem "quiet_assets", "~> 1.0.1"
gem 'thin'
gem 'bourbon'
gem "haml-rails"
gem "httparty", "~> 0.8.3"
gem "activerecord-import", "~> 0.2.10"
group :test, :development do
gem "rspec-rails", "~> 2.0"
gem 'capybara', '~>1.1.2'
gem "fabrication", "~> 2.2.0"
gem "launchy", "~> 2.1.2"
end
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end`
i'm pretty junior so go easy :) - THX !
capybara is not included in views spec, and it is for integration testing.
try to move your spec file into spec/requests directory
I had a similar issue and all i had to do was to add
config.include Capybara::DSL
to the spec_helper.rb file
Related
I have Rails 4.2.5 and rspec 3.4.1
When I add render_views some first controllers test are failed.
I use render_views because I don't know any methods to watch what happens on a page.
Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.5'
gem 'sqlite3'
gem 'haml-rails', '~> 0.9'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'twitter-bootstrap-rails'
gem 'less-rails'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
gem 'rspec-rails', '~> 3.0'
gem 'cucumber-rails', :require => false
gem 'database_cleaner'
gem 'factory_girl_rails'
gem 'shoulda-matchers'
gem 'capybara'
gem 'capybara-screenshot'
gem 'byebug'
end
group :development do
gem 'web-console', '~> 2.0'
gem 'spring'
end
gem 'devise'
gem 'cancan'
gem "role_model"
gem 'paperclip', '~> 4.3'
gem 'jquery-fileupload-rails'
gem 'stringex'
gem 'will_paginate'
gem 'russian', '~> 0.6.0'
spec/controllers/users_controller_spec.rb
RSpec.describe UsersController, type: :controller do
render_views
let(:valid_attributes) {
{email: "admin#example.com",
password: "password",
password_confirmation: "password"}
}
let(:invalid_attributes) {
}
let(:valid_session) { {} }
describe "GET #index" do
it "says 'Users'" do
get :index
end
end
...
end
spec/rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'devise'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
rspec spec/controllers/page_controller_spec.rb
PageController
GET #index
returns http success (FAILED - 1)
Failures:
1) PageController GET #index returns http success
Failure/Error: - if user_signed_in?
ActionView::Template::Error:
undefined method `authenticate' for nil:NilClass
# /home/ilp/.rvm/gems/ruby-2.1.4/gems/devise-3.5.3/lib/devise/controllers/helpers.rb:124:in `current_user'
# /home/ilp/.rvm/gems/ruby-2.1.4/gems/devise-3.5.3/lib/devise/controllers/helpers.rb:120:in `user_signed_in?'
# ./app/views/layouts/main.html.haml:36:in `_app_views_layouts_main_html_haml___2814915178728912122_59040000'
# ./spec/controllers/page_controller_spec.rb:11:in `block (3 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `authenticate' for nil:NilClass
# /home/ilp/.rvm/gems/ruby-2.1.4/gems/devise-3.5.3/lib/devise/controllers/helpers.rb:124:in `current_user'
Finished in 0.26407 seconds (files took 1.9 seconds to load)
1 example, 1 failure
app/controllers/page_controller.rb
class PageController < ApplicationController
def index
end
end
If I call sign_in or sign_our methods tests are success.
You need to call the sign_in method before rendering as it check the current_user object and calls the authenticate! method on current_user object which is nil so throwing error!
Do it like this
before do
sign_in_user
end
this will set the current_user object.
I started a fresh new rails app (using rspec-rails 2.99 & capybara 2.3.0), and wrote a silly simple integration spec to get started with TDD:
# spec/integration/visit_home_page_spec.rb
require 'spec_helper'
feature "view the home page" do
scenario "user visits home page" do
visit root_path
expect(page).to have_content 'Password'
end
end
As expected, the test fails due to root_path being undefined - so I went ahead and:
# config/routes.rb
root 'users#new'
And here's the problem: instead of the spec failing due to UsersController being undefined, it's green. It appears that rspec is evaluating the error page itself, and since that page happens to contain the word "password", the spec passes.
To make sure that's what's happening, I have modified the spec to:
# spec/integration/visit_home_page_spec.rb
...
expect(page).to have_content 'theresnowaythisisinthepage'
...
Now the test fails, but not because it's picking up the undefined UsersController - just because the content is not in the error page.
I've started many an app with this approach, and it's the first time I've seen rspec mistaking an error page for a valid one - what did I break?
I suspect my forehead will have a big hand-shaped mark when I find the answer.
--
Here's my Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 4.1.1'
gem 'mysql2', '~> 0.3.16'
gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'active_model_serializers', '~> 0.8.1'
gem 'slim-rails', '~> 2.1.4'
gem 'bootstrap-sass', '~> 3.1.1.1'
gem 'bootstrap-generators', '~> 3.1.1.3'
group :test, :development do
gem 'rspec-rails', '~> 2.99'
gem 'capybara', '~> 2.3.0'
end
group :development do
gem "better_errors"
gem "binding_of_caller" # required by better-errors
gem "launchy"
end
group :doc do
gem 'sdoc', require: false
end
Here's spec_helper
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.infer_spec_type_from_file_location!
config.include Capybara::DSL
end
when i change in env.rb file under feature/support
require 'cucumber/rails'
require "#{Rails.root}/spec/factories"
and in gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.13'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
# 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
group :test do
gem 'cucumber-rails'
gem 'capybara'
gem 'database_cleaner'
gem 'factory_girl_rails', :require => false
end
group :development, :test do
gem 'rspec-rails'
end
gem 'jquery-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'
# To use debugger
# gem 'debugger'
in spec/spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl_rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
in spec/factories.rb
FactoryGirl.define do
factory :user do
first_name "John"
last_name "Doe"
admin false
end
end
uninitialized constant FactoryGirl (NameError)
/home/manish/change/test_cucumber/spec/factories.rb:1:in `<top (required)>'
/home/manish/change/test_cucumber/features/support/env.rb:8:in `<top (required)>'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/rb_support/rb_language.rb:122:in `load'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/rb_support/rb_language.rb:122:in `load_code_file'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:180:in `load_file'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:83:in `block in load_files!'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:82:in `each'
In spec/spec_helper.rb try adding
FactoryGirl.find_definitions
under
require 'factory_girl_rails'
I also faced this problem in Hartl tutorial.
If you are using Spork to speed up your tests and it is running in the background, you need to stop it (Ctrl+C) and restart it ($ bundle exec spork).
Hi I have created a test case in spec/myprocess as follows
before :each do
#my_base = MyBase.new
end
describe "new_my_base" do
it "should not take any arguments and retuens a MyBase object" do
#my_base.should be_an_instance_of MyBase
end
end
describe "#my_path" do
it "returns the correct mybase file path" do
$path.should eql App::Application.config.my_encode_type.to_s
end
end
and when I tun rspec spec/lib/mybasetest.rb it throws me the following error.
kalanamith#kalanamith:~/Documents/projects/leena$ rspec spec/lib/mybasetest.rb
*** Mocha deprecation warning: Change `require 'mocha'` to `require 'mocha/setup'`.
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
*** Mocha deprecation warning: `require 'mocha/standalone'` has been deprecated. Please use `require 'mocha/api' instead.
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- mocha/object (LoadError)
/home/kalanamith/Documents/projects/fakturabank/spec/lib/mybasetest.rb:4:in `<top (required)>': undefined method `before' for main:Object (NoMethodError)
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `map'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'
This is my gem file
source 'https://rubygems.org'
gem 'rails', '3.2.13'
gem 'jquery-rails' , '3.0.0'
gem 'pg'
# 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'
gem 'uglifier', '>= 1.0.3'
end
group :development, :test do
gem 'rails-dev-tweaks', '~> 0.6.1'
gem 'debugger'
gem 'rspec-rails', '2.10.0'
gem 'annotate'
gem 'factory_girl_rails'
gem 'shoulda-matchers'
gem 'simplecov', :require => false
gem 'simplecov-rcov', :require => false
gem 'pry'
gem "guard-rspec"
end
group :test do
gem 'mocha'
end
group :test do
gem "webmock"
end
gem 'debugger' # This gem works with ruby 1.9.3 it wraps these other things
gem 'savon', "~> 1.2.0"
gem 'airbrake'
gem 'aasm'
gem 'paranoia'
gem 'acts_as_tree'
gem 'will_paginate'
gem 'translate-rails3'
gem 'wicked_pdf'
gem 'activemerchant'
gem 'libxml-ruby'
gem 'libxslt-ruby'
gem 'ruby-xslt'
gem 'exception_notification', :require => 'exception_notifier'
gem "uuidtools", "~> 2.1.3"
gem "ruby-filemagic"
gem "rmagick", :require => false
gem "rubyzip"
gem "spreadsheet"
gem "multi_json"
gem "multi_xml"
gem "httparty"
#FormBuilder backwards compatibility
gem "dynamic_form"
gem "prototype-rails"
gem "formtastic"
gem "net-ssh"
gem "net-scp"
Um quite new to rails and this is my first experience with rspec framework . I will be grateful if anyone can assist me. Thank you in advance
There must be a "describe" that wraps before :each do
describe "Bla bla" do
before :each do
#my_base = MyBase.new
end
etc...
end
Anyway, the file name should be suffixed with _spec.rb
The before block should be inside the describe method
If you're using RSpec the end of the file must be suffixed with _spec.rb
Example: my_base_spec.rb
To Run: bundle exec rspec spec/my_base_spec.rb or simply bundle exec rspec
I want to change my default drive for running tests with javascript, specific scenario with javascript, from default, :selenium, to any, like :webkit, in RSpec, just setting :js => true flag. But, trying to runing "describe 'xxx', :js => true do" with :webkit, and loading javascript_driver in rspec_helper, thats what happened "Undefined method javascript_driver for Capybara:Module"
undefined method `javascript_driver=' for Capybara:Module (NoMethodError)
how I defined loader :webkit in file spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Capybara.javascript_driver = :webkit
Version of Capybara is 1.1.2
Gem File:
source 'https://rubygems.org'
gem 'rails', '3.2.5'
gem 'pg', '0.12.2'
gem "meta_search"
# Bootstrap and layouting
gem 'bootstrap-sass', '2.0.3'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
gem 'rails3-jquery-autocomplete'
#test support
gem 'faker', '1.0.1'
#login rules
gem 'devise'
gem 'cancan'
#criptar
gem 'bcrypt-ruby', '3.0.1'
#BR
gem 'brazilian-rails'
gem 'rails-i18n'
group :development do
gem 'annotate', '~> 2.4.1.beta'
gem 'nested_scaffold'
end
group :development, :test do
gem 'rspec-rails', '2.11.0'
gem 'guard-rspec', '0.5.5'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
gem 'ruby-debug19'
gem 'linecache19'
gem 'factory_girl_rails', '1.4.0'
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 'capybara-webkit'
gem "launchy"
gem 'ZenTest'
#MAC OS Especific
gem 'rb-fsevent', '0.4.3.1', :require => false
gem 'growl', '1.0.3'
#Cucumber
gem 'cucumber-rails', '1.2.1', require: false
gem 'database_cleaner', '0.7.0'
end
group :production do
#gem 'pg', '0.12.2'
end
Thanks for you advanced.
missing this on spec_helper.rb
require "capybara/rspec"
Thanks https://github.com/jnicklas