rails 3.1.3 / cucumber / database_cleaner / mongo_mapper - ruby-on-rails-3.1

does anybody have also trouble running the cucumber tests in that envirounment?
Error
Exception encountered by DatabaseCleaner in Cucumber After block: ActiveRecord::ConnectionNotEstablished
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
/home/jonas/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in `retrieve_connection'
/home/jonas/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:in `retrieve_connection'
/home/jonas/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.2.1/lib/cucumber/rails/hooks/active_record.rb:6:in `connection'
/home/jonas/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.7.1/lib/database_cleaner/active_record/truncation.rb:130:in `clean'
/home/jonas/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.7.1/lib/database_cleaner/base.rb:77:in `clean'
/home/jonas/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.7.1/lib/database_cleaner/configuration.rb:56:in `block in clean'
/home/jonas/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.7.1/lib/database_cleaner/configuration.rb:56:in `each'
/home/jonas/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.7.1/lib/database_cleaner/configuration.rb:56:in `clean'
/home/jonas/.rvm/gems/ruby-1.9.2-p290/gems/cucumber-rails-1.2.1/lib/cucumber/rails/hooks/database_cleaner.rb:9:in `After'
features/support/env.rb
7 require 'cucumber/rails'
8 require 'database_cleaner'
9 require 'database_cleaner/cucumber'
10 require 'database_cleaner/mongo_mapper/truncation'
...
41 # Remove/comment out the lines below if your app doesn't have a database.
42 # For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
43 begin
44 DatabaseCleaner.strategy = :truncation
45 rescue NameError
46 raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
47 end
I got no idea what to do.

I'm getting the same problem, all my 'steps' pass but my Scenario fails due to the ActiveRecord mixup with Database Cleaner.
Scenario: User is not signed up # features/users/sign_in.feature:6
Given I am not logged in # features/step_definitions/user_steps.rb:86
And no user exists with an email of "user#invalidemail.com" # features/step_definitions/user_steps.rb:1
When I go to the sign in page # features/step_definitions/web_steps.rb:48
And I sign in as "user#invalidemail.com/please" # features/step_definitions/user_steps.rb:61
Then I should see "Invalid email or password." # features/step_definitions/web_steps.rb:105
And I go to the home page # features/step_definitions/web_steps.rb:48
And I should be signed out # features/step_definitions/user_steps.rb:81
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
/Users/ashr/.rvm/gems/ruby-1.9.3-p0#travel/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `retrieve_connection'
/Users/ashr/.rvm/gems/ruby-1.9.3-p0#travel/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
/Users/ashr/.rvm/gems/ruby-1.9.3-p0#travel/gems/cucumber-rails-1.3.0/lib/cucumber/rails/hooks/active_record.rb:6:in `connection'
/Users/ashr/.rvm/gems/ruby-1.9.3-p0#travel/gems/database_cleaner-0.7.2/lib/database_cleaner/active_record/truncation.rb:130:in `clean'
/Users/ashr/.rvm/gems/ruby-1.9.3-p0#travel/gems/database_cleaner-0.7.2/lib/database_cleaner/base.rb:77:in `clean'
/Users/ashr/.rvm/gems/ruby-1.9.3-p0#travel/gems/database_cleaner-0.7.2/lib/database_cleaner/configuration.rb:56:in `block in clean'
/Users/ashr/.rvm/gems/ruby-1.9.3-p0#travel/gems/database_cleaner-0.7.2/lib/database_cleaner/configuration.rb:56:in `each'
/Users/ashr/.rvm/gems/ruby-1.9.3-p0#travel/gems/database_cleaner- 0.7.2/lib/database_cleaner/configuration.rb:56:in `clean'
/Users/ashr/.rvm/gems/ruby-1.9.3-p0#travel/gems/cucumber-rails- 1.3.0/lib/cucumber/rails/hooks/database_cleaner.rb:9:in `After'
Failing Scenarios:
cucumber features/users/sign_in.feature:6 # Scenario: User is not signed up
1 scenario (1 failed)
7 steps (7 passed)
I am getting what may be a hint when executing my cucumber tests:
$bundle exec cucumber --verbose features/users/sign_in.feature:15
WARNING: You have set Rails' config.cache_classes to false (most likely in config/environments/cucumber.rb).
https://rspec.lighthouseapp.com/projects/16211/tickets/165 (related ticket)
Oddly it doesn't fail on the Before step.
spec_helper.rb
RSpec.configure do |config|
# == Mock Framework
config.mock_with :rspec
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
...
end
...
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
...
end
features/support/config.rb
Class ...
...
End
begin
DatabaseCleaner.strategy = :truncation, { :except => %w[oauth_tokens client_applications] }
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end

I had to remove DatebaseCleaner

Related

How can I force Rspec to not falsely think another module is nested inside the parent?

I have an issue that's appearing only when running rspec, my application loads and works fine locally and in production
rspec seems to think that the gem JWT belongs to my module BACKBONE
An error occurred while loading spec_helper.
Failure/Error: JWT.encode(payload, ENV['APP_SECRET'], 'HS256')
NameError:
uninitialized constant BACKBONE::JWT
# ./lib/jwt.rb:6:in `encode'
# ./lib/backbone/policies.rb:7:in `<class:Policy>'
# ./lib/backbone/policies.rb:5:in `<module:BACKBONE>'
# ./lib/backbone/policies.rb:3:in `<top (required)>'
# ./init.rb:10:in `load'
# ./init.rb:10:in `block in <module:CrewManagement>'
# ./init.rb:10:in `glob'
# ./init.rb:10:in `<module:CrewManagement>'
# ./init.rb:4:in `<top (required)>'
# ./spec/spec_helper.rb:16:in `require'
# ./spec/spec_helper.rb:16:in `<top (required)>'
files are listed in the order they are loaded into memory, it may be useful to note that I have several other applications with the same setup that are not experiencing this issue
/spec/spec_helper.rb
require 'bundler'
require 'simplecov'
SimpleCov.start
Bundler.require :default
Dotenv.load("./.env.#{(ENV['RACK_ENV'] || 'development')}")
require 'rack/test'
require './init'
# ...
/init.rb
module CrewManagement
## ...
Dir.glob('./lib/**/*.rb') { |file| load file }
## ...
end
/lib/jwt.rb
module BACKBONE
def self.encode(payload)
JWT.encode(payload, ENV['APP_SECRET'], 'HS256')
end
def self.decode(token)
JWT.decode(token, ENV['APP_SECRET'], true, algorithm: 'HS256')
end
end
/lib/backbone/policies.rb
module BACKBONE
headers = { 'Authorization' => BACKBONE.encode(app: '...') }
end

Mongoid and Rspec error Mongo::Error::NoServerAvailable:

When I am starting my Rspec test, I get this error:
1) User checks if the user is created
Failure/Error: expect{ #user_test = create(:user) }.to change { User.count }
Mongo::Error::NoServerAvailable:
No server is available matching preference: #<Mongo::ServerSelector::Primary:0x70316515164800 tag_sets=[] max_staleness=nil> using server_selection_timeout=30 and local_threshold=0.015
# /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongo-2.5.0/lib/mongo/server_selector/selectable.rb:115:in `select_server'
# /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongo-2.5.0/lib/mongo/collection/view/readable.rb:139:in `block in count'
# /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongo-2.5.0/lib/mongo/retryable.rb:44:in `read_with_retry'
# /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongo-2.5.0/lib/mongo/collection/view/readable.rb:138:in `count'
# /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/contextual/mongo.rb:70:in `block in count'
# /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/contextual/mongo.rb:504:in `try_cache'
# /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/contextual/mongo.rb:70:in `count'
# /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/contextual.rb:20:in `count'
# /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/findable.rb:55:in `count'
# ./spec/models/user_spec.rb:11:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:11:in `block (2 levels) in <top (required)>'
Finished in 30.14 seconds (files took 8.39 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:10 # User checks if the user is created
I am really confused about it.
My mongo server is running too.
My user_spec.rb:
require 'spec_helper'
require 'rails_helper'
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
RSpec.describe User, :type => :model do
it "checks if the user is created" do
expect{ #user_test = create(:user) }.to change { User.count }
end
end
Any idea why my mongo won't work?
Found the answer
My config file had mistake so I change it to :
test:
clients:
default:
database: test
hosts:
- 127.0.0.1:27017
options:
read:
mode: :primary
max_pool_size: 1
log_level: :debug

Facing this issue #driver.quit giving error in after (:context ) in rspec

I am trying this
require 'rspec'
require 'selenium-webdriver'
RSpec.describe 'New test' do
before :each do
#driver = Selenium::WebDriver.for (:firefox)
end
after :all do
#driver.quit
end
it 'should signup and create a new user' do
p 'testing'
end
end
And this is the error I get
rspec test.rb [13:04:18]
"testing"
.
An error occurred in an `after(:context)` hook.
Failure/Error: #driver.quit
NoMethodError:
undefined method `quit' for nil:NilClass
# ./test.rb:11:in `block (2 levels) in <top (required)>'
Finished in 4.61 seconds (files took 0.17828 seconds to load)
1 example, 0 failures
I am sure it is a small thing that i am missing but could not find it any help would be appreciated.
Is this a FF or selenium version issue?
I am using FF 45 and selenium 2.53 and rspec 3.5.4.
This is an RSpec issue. before/after(:all) hooks only have access to instance variables defined in their scope or outer scope levels, but not to instance variables declared in the current spec being run. after(:each) hooks do have access to the instance variables declared in the current spec.
before :all do
#a = 2
end
context "blah" do
before :all do
#b = 3
end
it "blah blah" do
#c = 4
# #a,#b,#c all accessible here
end
after :each do
##a, #b, #c accessible here
end
after :all do
# #a,#b accessible here
end
end
after :all do
#can access #a
end

rake spec failed, it cannot load or find constant or global variable

I create standalone rspec test script to testing existing api framework. It works pretty well, but I found problem where in the Rakefile I need to assign some value from YAML file (uri link, email) either CONSTANT or $global_var the code in the Rakefile looks like this:
require 'rubygems'
require 'bundler/setup'
require 'yaml'
require 'rspec/core/rake_task'
task :default => :spec
desc 'Running rspec test'
task :spec, :option do |t, opt|
choice = opt[:choice]
if choice == "production"
puts 'Test running on production'
VAR = YAML::load(File.read(File.expand_path("../config/prod_variable.yml", __FILE__)))
elsif choice == "development"
puts 'Test running on development'
VAR = YAML::load(File.read(File.expand_path("../config/dev_variable.yml", __FILE__)))
end
puts VAR['URI'] #=> print out the value correctly
RSpec::Core::RakeTask.new do |task|
test = Rake.application.original_dir
task.fail_on_error = false
task.rspec_opts = '--format documentation --color'
end
end
When I run the rake command on the terminal, the rspec failed find the VAR constant value. Here is the error message from rspec
Failures:
1) ApiTest Testing API platform for GET request
Failure/Error: #var = ApiTest.new(VAR['URI'] ,
NameError:
uninitialized constant VAR
# ./rspec_test/api_test/api_test_get_spec.rb:8:in `block (2 levels) in <top (required)>'
2) ApiTest Testing API platform for POST request
Failure/Error: #zat = ApiTest.new(VAR['URI'] ,
NameError:
uninitialized constant VAR
# ./rspec_test/api_test/api_test_post_spec.rb:7:in `block (2 levels) in <top (required)>'
Is there any idea how to get this works? I need to get value from VAR constant or global variable, but seems ruby failed to assign the value.
If opt[:choice] is neither "production" nor "development", VAR is undefined in your code.

undefined method `infer_base_class_for_anonymous_controllers=' for #<RSpec::Core::Configuration:0x007f7fb3a62b80> (NoMethodError)

I'm currently on Chapter 7 of Hartl's Tutorial, and every time I run
bundle exec rspec spec/
the following results:
/Users/siciliana/sample_app/spec/spec_helper.rb:31:in `block in <top (required)>': undefined method `infer_base_class_for_anonymous_controllers=' for #<RSpec::Core::Configuration:0x007f7fb3a62b80> (NoMethodError)
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core.rb:79:in `configure'
from /Users/siciliana/sample_app/spec/spec_helper.rb:11:in `<top (required)>'
from /Users/siciliana/sample_app/spec/models/user_spec.rb:12:in `require'
from /Users/siciliana/sample_app/spec/models/user_spec.rb:12:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `load'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `map'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `load_spec_files'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:18:in `run'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:80:in `run_in_process'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:69:in `run'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:11:in `block in autorun'
Can someone please explain what is happening here to an absolute newbie?
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'
#
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# 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
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# 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"
end
I was getting the same error (although not following the tutorial).
I believe this setting requires 'rspec/rails'
My solution was to move the line
config.infer_base_class_for_anonymous_controllers = # whatever
From spec_helper.rb to rails_helper.rb.

Resources