I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2. I would like to use Selenium in order to test JavaScripts, but without to delete the test database data each time I run the cucumber command line in my Terminal window. That is, if I state a feature like the following:
Feature: ...
#javascript
Scenario: ...
JavaScript is tested as well as expected. However, after the test has run, the test database data is deleted and I must seed again that database in order to properly run new tests.
I read the Official Documentation and the text present in the ROOT_APP/features/support/env.rb file (it seems that I installed all required Ruby gems - see below for more information about the Gemfile that I am using) but I didn't understand how to avoid to delete the database data and how to configure Cucumber and Capybara gems so to properly work with Selenium.
What should I make?
Note I: I would like to make the above because I would like to have the same test database data when I "test"/"run" Scenarios.
Note II: In order to seed data in the test database (my application needs that data to work), I add the following code in the RAILS_ROOT_PATH/lib/tasks/cucumber.rake file and I run the rake db:test:prepare command line from the Terminal window.
namespace :db do
namespace :test do
task :prepare => :environment do
Rake::Task["db:seed"].invoke
end
end
end
In the ROOT_APP/features/support/env.rb file I tried to uncomment one and both of the following blocks of code (BTW: I never changed the original file auto-generated by the cucumber-rails gem, so it is the default one), but after running tests it still deletes the test database data.
# Before('#no-txn,#selenium,#culerity,#celerity,#javascript') do
# # { :except => [:widgets] } may not do what you expect here
# # as tCucumber::Rails::Database.javascript_strategy overrides
# # this setting.
# DatabaseCleaner.strategy = :truncation
# end
#
# Before('~#no-txn', '~#selenium', '~#culerity', '~#celerity', '~#javascript') do
# DatabaseCleaner.strategy = :transaction
# end
Gemfile excerpt:
group :development, :test do
gem "rspec-rails"
end
group :test do
gem 'cucumber-rails'
gem 'database_cleaner'
gem 'capybara'
end
I ran into this same problem, and managed to fix it by changing the following line in ROOT_APP/features/support/env.rb
from
Cucumber::Rails::Database.javascript_strategy = :truncation
to
Cucumber::Rails::Database.javascript_strategy = :transaction
Hope this helps...
Related
I have capybara and when I run my Featured tests raised an error, it says about pending migrations in test environment, but when a run other type of test everything is good. I've already run all my migrations in test environment, I've already run rails in test and development envs.
this is my gemfile
group :development, :test do
.... other gems ....
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'database_cleaner'
gem 'capybara', '~> 2.5.0'
gem 'capybara-webkit', '~> 1.7.1'
gem 'selenium-webdriver'
gem 'poltergeist'
gem 'launchy-rails', '~> 0.0.1'
end
this is my test
# spec/features/sign_in_spec.rb
require 'rails_helper'
feature 'Visitor signs up' do
it "signs me in", :type => :feature do
visit new_user_session_path
puts "page: #{page.html.inspect}"
save_and_open_page
end
end
Thanks!
UPDATE:
I just try with this command:
bin/rake db:drop db:create db:migrate RAILS_ENV=test
Everything is ok with that command. Then I run my server with:
rails s -e test
Same error in my browser, "ActiveRecord::PendingMigrationError"
http://localhost:3000/
Then I run migrate command
bin/rake db:migrate RAILS_ENV=test
But it raised an error "ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR: relation "users" already exists"
And same error when a I run the features tests.
Here are my helpers: https://gist.github.com/israelb/e2f4b10ba5f94e1e8df2
There may be some kind of migration issue. Try rake db:test:prepare see if that helps.
I found the solution, I was a problem in my config/enviroment/test.rb file, I had to change by this one:
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
# Configure static file server for tests with Cache-Control for performance.
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
# Randomize the order test cases are executed.
config.active_support.test_order = :random
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
I'm using travisCI to deploy to heroku and I am getting this error. It has only just started happening.
I have the basic rails Rakefile and I have a file that looks like this as otherwise travis cannot detect the rake tasks:
# lib\tasks\spec.rake
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task :default => :spec
Why would this error be displaying specifically for heroku?
EDIT - I had a similar version to the (better) answer given:
begin
require 'rspec/core/rake_task'
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
t.pattern = 'spec/*_spec.rb'
end
rescue LoadError
end
If rspec isn't in the production group (it generally isn't) then the code you posted would fail when run in a production environment like heroku.
In the rspec docs they recommend doing this:
begin
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
rescue LoadError
end
So that the absence of rspec doesn't stop your rakefile loading.
FWIW, I just faced the same issue. I fixed it by moving rspec-rails to production as shown below. I don't know why it works, but it works for me.
group :production, :development, :test do
gem 'rspec-rails', '~> 3.8'
end
Surprisingly, I didn't find the standard way to make the output of Rails Minitest colorful. There're some workarounds, though.
So what's the way to do that?
Add minitest-rg to the test group in your Gemfile:
group :test do
gem "minitest-rg"
end
Then require minitest/rg in your test/test_helper.rb:
require "minitest/rg"
Now you have colorful test output when running rake test.
Sure, in this rakefile change it to:
namespace :test do
task :isolated do
Dir.glob("test/**/*_test.rb").all? do |file|
sh(Gem.ruby, '-w', '-Ilib:test', file, '-p')
end or raise "Failures"
end
end
Then from the console run rake test:isolated while in activesupport folder and watch the rainbow go! This is using minitests built in pride library for color.
I'm using rspec-rails to test a controller in my rails application. In one instance, I'm creating new objects to test that they're properly assigned to instance variables.
describe MainController do
describe "GET #index" do
it "properly assigns a colored ball" do
ball1 = Ball.create(color: 'red')
get :index
expect(assigns(:red_balls)).to eq([ball1])
end
end
end
When I run the test, does rspec access the database to create ball1 and then delete it once the test is run?
From the snippet in your question, Yes Rspec access the DB configured as test in the database.yml file, if you want to clean out the DB/Records created by Rspec you can use the database_cleaner gem you can configure it as you deem fit.
e.g:
add gem database_cleaner to your Gemfile
and the below to your spec_helper.rb file as specified by the database_cleaner documentation:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Then database_cleaner would truncate the tables in the test db and leave them in a pristine state
To add to what bjhaid said above - Rspec should only be accessing you 'test' database. Check out the config/database.yml to see the configuration of your different databases.
So you do not have to worry about things in your development or production databases interfering with your testing database.
Yes, RSpec allows your database calls to go through, but with the rspec-rails gem under the default configuration, it will execute each test within a database transaction and will roll back that transaction at the end of each example with no action required on your part. No additional gem is required. See https://relishapp.com/rspec/rspec-rails/docs/transactions.
I am new to rails. i try to write test for a model for that i use factory-girl gem. In that data was taken from XML file.
My problem is when ever am running my rspec file, data was appended every time, in XML file i have only 32 data, but every time am executing rsepc data was increasing...
i even tried database_cleaner but same result.
I want to delete the data in factory-girl.
is there anyway to avoid duplication in factory-girl?
is there anyway to use where condition like query for factory-girl?
Thank you.
Try this:
The following things use to reset factory girl data.
Add following line in your Gemfile and try bundle install.
gem "database_cleaner", ">= 0.8.0", :group => :test
In spec_helper.rb:
RSpec.configure do |config|
# Other things
# Clean up the database
require 'database_cleaner'
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
end
config.before(:each) do
DatabaseCleaner.clean
end
end