First i read other posts of users with similiar problems, but couldnt come along where my mistake is. I wanted to start a test with RSpec on the following file:
dashboard_view_spec.rb:
require 'rails_helper'
RSpec.feature "Dashboard", type: :feature do
before(:each) do
#current_user = User.create!(email: "xyz#xyz.com", password: "xyz123")
sign_in_with(#current_user.email,#current_user.password)
end
#NAV BAR RSPEC TEST
scenario 'Home bar nav link present' do
visit "/"
expect(page).to have_text('Home')
end
scenario 'How it work nav bar link present' do
visit "/"
expect(page).to have_text('How it work')
end
scenario 'Support nav bar link present' do
visit "/"
expect(page).to have_text('Support')
end
end
On rails_helper.rb on the top:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rake'
require 'spec_helper'
require 'rspec/rails'
require 'shoulda/matchers'
require 'capybara/rails'
require 'capybara/rspec'
require 'rspec/retry'
require 'devise'
Error message:
Failure/Error: require File.expand_path('../../config/environment', __FILE__)
NoMethodError:
undefined method `[]' for nil:NilClass
# ./config/initializers/devise.rb:258:in `block in <top (required)>'
# ./config/initializers/devise.rb:5:in `<top (required)>'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `require'
# ./spec/rails_helper.rb:4:in `<top (required)>'
# ./spec/views/dashboard_view_spec.rb:1:in `require'
# ./spec/views/dashboard_view_spec.rb:1:in `<top (required)>'
No examples found.
Randomized with seed 14549
Then the command i used on the terminal
bundle exec rspec spec/views/dashboard_view_spec.rb
After watching the documentation of testing with Devise i changed the code in dashboard_view_spec.rb and used to sign_in as a user and got the same error message.
Line 258 of devise.rb
config.omniauth :facebook, Rails.application.secrets.facebook[:key], Rails.application.secrets.facebook[:secret], scope: 'email', info_fields: 'email, name'
In the gemfile
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
gem 'rspec-rails', '~> 3.8'
gem 'factory_girl_rails'
gem 'faker'
gem 'database_cleaner'
end
and
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
The problem is that you're missing a variable that your devise initializer expects to find, but only missing it in the test environment. (not the development environment)
The stack trace that you provided and line 258 from config/initializers/devise.rb are all that are needed. First, let's look at the stack trace:
NoMethodError:
undefined method `[]' for nil:NilClass
# ./config/initializers/devise.rb:258:in `block in <top (required)>'
# ./config/initializers/devise.rb:5:in `<top (required)>'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `require'
# ./spec/rails_helper.rb:4:in `<top (required)>'
# ./spec/views/dashboard_view_spec.rb:1:in `require'
# ./spec/views/dashboard_view_spec.rb:1:in `<top (required)>'
Starting from the bottom up, we can see that the first line of your spec is what's causing the issue. If you follow the stack you'll see that it ends up in config/initializers/devise.rb:258 and is complaining that something making a [] method call isn't getting the expected object type in return, and is instead getting nil in return.
Looking at line 258 you find:
config.omniauth :facebook, Rails.application.secrets.facebook[:key], Rails.application.secrets.facebook[:secret], scope: 'email', info_fields: 'email, name'
So you can see that there are two times that [] is called:
Rails.application.secrets.facebook[:key]
Rails.application.secrets.facebook[:secret]
What is expected is that calling Rails.application.secrets.facebook will return an Enumerable object (like an array or a hash) but instead it is returning nil, and when it attempts to run nil[:key] or nil[:secret] it raises an exception because NilClass is not an Enumerable and does not have a [] method. You can test this yourself in the rails console:
nil[:secret]
=> NoMethodError: undefined method `[]' for nil:NilClass
The solution is to ensure that calling Rails.application.secrets.facebook returns the expected object type. The short answer is to edit config/secrets.yml to ensure that the values you require for the test environment are present.
I haven't worked with devise in a long time, but I assume that you can safely use the same values that you're using for the development environment for the test environment. You can read more about secrets() elsewhere, but the basic template for config/secrets.yml is as follows:
environment:
key: value
For example:
test:
my_secret_key: my_secret_value
It should be fairly straightforward to copy and paste the missing facebook secrets into the test environment. After making the change you can verify it worked:
$ RAILS_ENV=test rails console
Rails.application.secrets.facebook[:key]
=> <your key here>
If that worked, then run your spec with rspec and it should successfully get past line 1. (assuming there are no other bugs or missing secrets)
I had very similar errors. Here's how I solved
Run rspec --backtrace
This could produce a lot of console output. Scroll right to the top, and start reading from the top. It will tell you the file your problem started in, and the line it happened on. Go to that file (and that line), and figure out what's missing.
In my case, it was actually a missing variable in credentials.yml, but cases will vary. Like in the excellent answer by #anothermh, it's basically telling you something is missing, so you have to figure out what's missing and make sure you provide it.
Just got the same error, but when I ran bundle exec rake spec instead of bundle exec rspec it worked.
Related
hope somebody can help me with this. I did search but haven't found any working solution.
I've started writing test for an app. My integration tests run fine, but then I decided that since I'm not that much of TDD driven and since I don't have that much time right now to extensively test all layers of the app that I should use instead of integration tests system tests, because they allow me to test the full flow as if in a browser.
Rails 5.1.2
Gemfile
(tried different variations, just capybara, then with combinations of both the other two)
gem 'minitest-rails'
gem 'minitest-rails-capybara'
gem 'capybara'
test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
EMPTY_NEW_USER = {
email: '',
first_name: '',
last_name: '',
username: '',
password: ''
}
EXISTING_USER = {
email: '****',
first_name: 'John',
last_name: 'Doe',
username: '',
password: 'testingpass',
password_confirmation: 'testingpass'
}
# Add more helper methods to be used by all tests here...
end
application_system_test_case.rb
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
end
register_logins.rb
require "application_system_test_case"
class RegisterLoginsTest < ApplicationSystemTestCase
test 'full login flow' do
visit root_url
assert_response :success
find('.email_link').click
end
end
error when running
rake test:system
LoadError: cannot load such file -- capybara/minitest
/Users/mnussbaumer/code/dvouch/test/application_system_test_case.rb:3:in `<top (required)>'
/Users/mnussbaumer/code/dvouch/test/system/register_logins_test.rb:1:in `<top (required)>'
Tasks: TOP => test:system
(See full trace by running task with --trace)
The full trace adds this:
LoadError: cannot load such file -- capybara/minitest
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `block in require'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.1.2/lib/action_dispatch/system_test_case.rb:2:in `<top (required)>'
/Users/mnussbaumer/code/dvouch/test/application_system_test_case.rb:3:in `<top (required)>'
and goes on on active_support dependencies.
What I have tried:
Adding one, two and three to test_helper.rb:
require "capybara/rails"
require "minitest/rails"
require "minitest/rails/capybara"
I tried with gems:
group :development, :test do
gem 'minitest-rails'
gem 'minitest-capybara'
gem 'capybara'
end
then with 'minitest-rails-capybara'
Thanks
The file capybara/minitest was added to Capybara in version 2.13.0, which is the minimum version Rails requires for its system tests since Rails 5.1.0. Upgrade to the latest version of Capybara (2.14.4) and there should be no need for the minitest-capybara or minitest-rails gems. You will need to also add the 'selenium-webdriver' gem to your test group.
Additionally the assert_response :success line is't valid in Capybara tests because the HTTP response code from the browser Capybara is using isn't generally available.
I am trying to setup rspec (v3.0.2) on Rails 4.1.9 with factory_girl_rails but after implementing rails_helper.rb and spec_helper.rb I get the following error when running my _spec.rb file:
/Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:9:in `<module:AttributeMethods>': uninitialized constant ActiveModel::AttributeMethods (NameError)
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:7:in `<module:ActiveRecord>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:5:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record.rb:101:in `<module:ActiveRecord>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record.rb:31:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/railtie.rb:1:in `require'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/railtie.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:12:in `require'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:12:in `block in <top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:10:in `each'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:10:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/config/application.rb:3:in `require'
from /Users/user/code/rtb-campaign-optimization/config/application.rb:3:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/config/environment.rb:2:in `require'
from /Users/user/code/rtb-campaign-optimization/config/environment.rb:2:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/spec/rails_helper.rb:4:in `require'
from /Users/user/code/rtb-campaign-optimization/spec/rails_helper.rb:4:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/spec/models/cpa_calculator_spec.rb:1:in `require'
from /Users/user/code/rtb-campaign-optimization/spec/models/cpa_calculator_spec.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `block in load_spec_files'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `each'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load_spec_files'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:97:in `setup'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:85:in `run'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `load'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `<main>'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
Here is my gemfile:
group :development do
gem 'rails-dev-tweaks'
gem 'quiet_assets'
gem 'pry'
gem 'pry-byebug'
gem 'hirb'
gem 'spring'
gem 'spring-commands-rspec'
end
group :development, :test do
gem 'rspec-rails', '~> 3.0.0'
gem 'factory_girl_rails'
end
group :test do
gem 'database_cleaner'
end
rails_helper.rb:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
end
spec_helper.rb:
RSpec.configure do |config|
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
expectations.syntax = :expect
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = :expect
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end
end
my _spec file:
require 'rails_helper'
describe 'CampaignsManagement::CpaCalculator' do
let(:campaign) {
camp = FactoryGirl.build_stubbed(:campaign)
# campaign_histories_per_campaigns.times.each do |index|
# Factory.build(:campaign_history, value: index + 1, campaign_id: camp.id)
# end
camp
}
context "when there are some active campaigns" do
context "when the campaign have histories" do
let(:campaign_histories_per_campaigns) { 10 }
context 'when the campaign have histories created today' do
end
context 'when the campaign has only histories in the past (not generated today)' do
end
end
context "when the campaign is histories orphaned" do
let(:campaign_histories_per_campaigns) { 0 }
it "should not update redshift" do
campaign.campaign_histories.size.should.eq(0)
end
end
end
end
application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module RtbCampaignOptimization
class Application < Rails::Application
config.assets.enabled = false
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.autoload_paths << Rails.root.join('lib')
end
end
any ideas?
Thanks
Try to run:
rails generate rspec:install //which you must have already run
bundle install
And also check out this question and it's answers
According to the above, you need to add spork-rails to your Gemfile.
All the above-shared code snippets all look good.
When I run rspec test I get:
/home/jasiek/Desktop/katowice-ror-workshops-2015/spec/support/features.rb:2:in `block in ': uninitialized constant Features (NameError)
I suppose problem is only on my local machine because this is repo from trust source (another users haven't got this problem). The repo I'm talking about: https://github.com/netguru-training/katowice-ror-workshops-2015
I read on Stack that the problem could be missing line:
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
But I've got it.
Where could be a problem?
And this is a whole log from console:
jasiek#jasiek-HP-EliteBook-8470p:~/Desktop/katowice-ror-workshops-2015$ RAILS_ENV=test bundle exec rspec
/home/jasiek/Desktop/katowice-ror-workshops-2015/spec/support/features.rb:2:in block in <top (required)>': uninitialized constant Features (NameError)
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core.rb:97:inconfigure'
from /home/jasiek/Desktop/katowice-ror-workshops-2015/spec/support/features.rb:1:in <top (required)>'
from /home/jasiek/Desktop/katowice-ror-workshops-2015/spec/rails_helper.rb:23:inblock in '
from /home/jasiek/Desktop/katowice-ror-workshops-2015/spec/rails_helper.rb:23:in each'
from /home/jasiek/Desktop/katowice-ror-workshops-2015/spec/rails_helper.rb:23:in'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration.rb:1280:in require'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration.rb:1280:inblock in requires='
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration.rb:1280:in each'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration.rb:1280:inrequires='
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration_options.rb:109:in block in process_options_into'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration_options.rb:108:ineach'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration_options.rb:108:in process_options_into'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration_options.rb:21:inconfigure'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/runner.rb:101:in setup'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/runner.rb:88:inrun'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/runner.rb:73:in run'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/runner.rb:41:ininvoke'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/exe/rspec:4:in <top (required)>'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/bin/rspec:23:inload'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/bin/rspec:23:in <main>'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/bin/ruby_executable_hooks:15:ineval'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/bin/ruby_executable_hooks:15:in `'
I know its late.
But for other people who may come here for searching the answer. I was facing the same issue.
So when I checked rails_helper.rb file inside spec I found that this line
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
was commented initially so I un-commented it and everything worked fine.
I am using
gem 'capybara', '~> 2.15', '>= 2.15.4'
gem 'rspec-rails', '~> 3.6'
Try adding the following in your spec/support/features.rb file:
require '../../spec/support/features/session_helpers'
So, it becomes:
require '../../spec/support/features/session_helpers'
RSpec.configure do |config|
config.include Features::SessionHelpers, type: :feature
end
I want to output a screen shot of the page after a failed scenario.
I'm using capybara, rspec and launchy - my gemfile has
group :development, :test do
gem 'rspec-rails'
gem 'capybara'
gem 'launchy'
end
I've seen various suggestions which include the following code
After do |scenario|
save_and_open_page if scenario.failed?
end
So, I've created a spec\support\env.rb file into which I've put this code (and that is all, there is nothing else in that file). Now when I run
bundle exec rspec
I get
C:/Working/x/spec/support/env.rb:1:in `<top (required)>': undefined method `After' for main:Object (NoMethodError)
If I comment out the lines in env.rb, then the tests run as I'd expect.
What am I missing?
Thanks
Versions:
rails 4
rspec 2.14.1
capybara 2.1.0
launchy 2.3.0
Try wrapping it in a Rspec.configure block, something like:
RSpec.configure do |config|
config.after { |example_group| save_and_open_page if example_group.exception }
end
#ejosafat - extra "example." in you answer, so I used it as following:
RSpec.configure do |config|
config.after { |example_group| save_and_open_page if example_group.exception }
end
There is a gem, you just need to add this to your Gemfile:
gem 'capybara-screenshot', :group => :test
All the instructions can be found here:- https://github.com/mattheworiordan/capybara-screenshot
I have a rails app with Cucumber 1.0.0 and cucumber-rails 1.0.2. These tests ran fine last night. Today I just altered some step definitions and tried to run the tests and got the error below. I haven't changed anything in the cucumber support files. Any ideas?
grouperty $ cucumber
Using the default profile...
no such file to load -- /Users/davidtuite/wd/grouperty/features/config/environment(LoadError)
/Users/davidtuite/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/davidtuite/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/davidtuite/wd/grouperty/features/support/env.rb:12:in `block in <top (required)>'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/spork-0.9.0.rc9/lib/spork.rb:24:in `prefork'
/Users/davidtuite/wd/grouperty/features/support/env.rb:10:in `<top (required)>'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/rb_support/rb_language.rb:143:in `load'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/rb_support/rb_language.rb:143:in `load_code_file'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime/support_code.rb:176:in `load_file'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime/support_code.rb:78:in `block in load_files!'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime/support_code.rb:77:in `each'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime/support_code.rb:77:in `load_files!'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime.rb:137:in `load_step_definitions'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime.rb:39:in `run!'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/cli/main.rb:43:in `execute!'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/cli/main.rb:20:in `execute'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/bin/cucumber:14:in `<top (required)>'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/bin/cucumber:19:in `load'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/bin/cucumber:19:in `'
Here is the content of /features/support/env.rb. Note that most of it was generated by the spork gem (v0.9.0.rc9).
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
# It is recommended to regenerate this file in the future when you upgrade to a
# newer version of cucumber-rails. Consider adding your own code to a new file
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'cucumber/rails'
# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
# order to ease the transition to Capybara we set the default here. If you'd
# prefer to use XPath just remove this line and adjust any selectors in your
# steps to use the XPath syntax.
Capybara.default_selector = :css
end
Spork.each_run do
# By default, any exception happening in your Rails application will bubble up
# to Cucumber so that your scenario will fail. This is a different from how
# your application behaves in the production environment, where an error page will
# be rendered instead.
#
# Sometimes we want to override this default behaviour and allow Rails to rescue
# exceptions and display an error page (just like when the app is running in production).
# Typical scenarios where you want to do this is when you test your error pages.
# There are two ways to allow Rails to rescue exceptions:
#
# 1) Tag your scenario (or feature) with #allow-rescue
#
# 2) Set the value below to true. Beware that doing this globally is not
# recommended as it will mask a lot of errors for you!
#
ActionController::Base.allow_rescue = false
# Remove/comment out the lines below if your app doesn't have a database.
# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
begin
DatabaseCleaner.strategy = :transaction
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
# See the DatabaseCleaner documentation for details. Example:
#
# Before('#no-txn,#selenium,#culerity,#celerity,#javascript') do
# DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
# end
#
# Before('~#no-txn', '~#selenium', '~#culerity', '~#celerity', '~#javascript') do
# DatabaseCleaner.strategy = :transaction
# end
#
end
this line
require File.expand_path("../../config/environment", __FILE__)
requires
/Users/davidtuite/wd/grouperty/features/config/environment
which is not present there this causes error