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
Related
When I run my rspec tests, many fail due to stale data in my mongodb database. AFAIK it is far better to test with a clean database.
How can I clean and/or re-seed the database before each test?
You can use database_cleaner gem to accomplish this task.
From their documentation:
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do |example|
DatabaseCleaner.strategy= :truncation
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
As you are using Mongoid ORM, you may also need to specify it explicitly:
# How to setup your ORM explicitly
DatabaseCleaner[:mongoid].strategy = :truncation
Update:
I see an open issue for MongoID 5
To make it work, you can monkey patch the Mongo Ruby driver class as mentioned in the issue.
module Mongo
class Collection
class View
def remove_all
remove(0)
end
end
end
end
Athough it's not a great solution!
The fix to the problem is there in the master branch of database_cleaner(1.4.1) gem. Install the gem from master to fix the problem (until there is the version bump). Expected to get fixed in next version.
gem 'database_cleaner', :git => 'https://github.com/DatabaseCleaner/database_cleaner.git'
I included database_cleaner gem in my rails app. Followed the example given on the git repo and included the following code in spec_helper :
Approach 1
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
When i run the rspec i get error as NoMethodError:undefined method 'cleaning' for DatabaseCleaner:Module.
So i did some research and found that i could replace the config.around block above with something like this :
Approach 2
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
OR
Approach 3
config.around(:each) do |example|
DatabaseCleaner.start
example.run
DatabaseCleaner.clean
end
Both Approach 2 and 3 work well.
I also looked in the git repo of database_cleaner and found that cleaning method actually exists and with the following code :
def cleaning(&block)
start
yield
clean
end
which is exactly same as what i did in example 3. If it does exists then why is it not accessible? Am i missing something here. Any more setup?
Or is Approach 2 or 3 preferable?
Finally found the answer,
database_cleaner gem added the cleaning method just last week and also updated the documentation for the same. BUT this change is not available in latest gem version 1.2.0 which I sourced from rubygems.org. Approach 1 works perfectly when i source the gem from github as below:
gem 'database_cleaner', git: 'git#github.com:DatabaseCleaner/database_cleaner.git'
You can use the approach in the documentation if you pull the gem from Github
gem 'database_cleaner', git: 'git#github.com:bmabey/database_cleaner.git'
If you have this same issue using mongoid you can add this to Gemfile, change version to suit you, and run bundle install.
gem 'database_cleaner', '~> 1.4.1'
Then create a support folder in
spec/support/database_cleaner.rb
Require database_cleaner.rb in your spec_helper file, I use gem 'require_all' like so:
# spec/spec_helper.rb
require 'require_all'
require_rel 'support'
Add the following cleaners to database_cleaner.rb
RSpec.configure do |config|
# Cleanup the DB in between test runs
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
DatabaseCleaner[:mongoid].clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Your tests should now tear down properly.
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.
When running our rspec suite of tests
bundle exec rspec spec/
The logs are cluttered with far to many log statements. In particular, the controller specs show things like this multiple times:
{"controller"=>"myController", "action"=>"create"}
I would like to get rid of these but can't find the source. There are no puts statements which match anything like this nor are there any Rails.logger calls. I'm assuming this is a log level issue but I could be wrong. Setting config.log_level in environment/test.rb has no effect.
The current rspec configuration looks like this
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
config.mock_with :rspec
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
end
config.before(:each) do
DatabaseCleaner.clean
end
config.color_enabled = true
config.tty = true
config.formatter = :documentation # :progress, :html, :textmate
end
Any thoughts on how I might disable these type of logs?
Side note: Gemfile is using 'rails', '3.2.13' and 'rspec-rails', '2.14.0'
In case someone comes across this same thread later on, I found that I had the same problem the author described, however, it came from having the Heroku rails_12factor gem in my Gemfile.
Another user said that the gem was causing double output for them: Double console output?
As soon as I either commented it out or put it in the :production group, all of the verbose SQL output in my console went away.
So just another thing to check if you have the same problem, but the author's solution isn't what fixes it for you.
The issue was not with Rspec after all. Instead Someone had written 'p params' in a controller helper. This question is not really valid due to this.
Relishapp's docs were very useful in uncovering this
https://www.relishapp.com/rspec/rspec-rails/docs
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...