Problems with rspec 3, capybara and machinist 2 - capybara

I have this feature test (Store model have a uniqueness validation):
feature "Index" do
before do
3.times { Store.make! }
end
scenario "User visit index" do
visit stores_path
within("table.table") do
Store.all.each do |store|
expect(page).to have_content(store.name)
end
end
end
end
When i run the test, randomly fails:
Failure/Error: 3.times { Store.make! }
ActiveRecord::RecordInvalid:
Validation failed: name has already been taken
My blueprint is (follow the machinist documentation to generate a unique record):
Store.blueprint do
name { "store #{sn}" }
end
I use DatabaseCleaner gem, then my config rails_spec:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
require 'capybara/rails'
require 'capybara/rspec'
require 'pundit/rspec'
require 'database_cleaner'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.before(:each) do |example|
DatabaseCleaner.strategy = if example.metadata[:js]
:truncation
else
:transaction
end
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
DatabaseCleaner.logger = Rails.logger
config.infer_spec_type_from_file_location!
That errors are driving me crazy

Try to replace the following in your spec/spec_helper.rb:
config.before :suite do |example|
DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
end
config.before do
DatabaseCleaner.clean
end

Related

How to configure database_cleaner with rails

I have some services inside my app/services folder.Now I want to test these services, so I am using rspec and factorygirl. I am creating user with factoryGirl, but when i am running rspec it is showing me following error
Failure/Error: #user ||= FactoryGirl.create(:user)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
I am using database_cleaner gem to clean the database after each test.
Below is my config file
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 'factory_girl_rails'
require 'database_cleaner
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include Devise::TestHelpers, type: :controller
config.use_transactional_fixtures = false
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
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
I don't know where i am going wrong.
You need to specify the strategy to use between each specs (transaction is a safe choice here):
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end

`autodetect': No known ORM was detected

Unable to clean data using database_cleaner.rb; throwing the following issue on running tests.
/Users/prashanth_sams/.rvm/gems/ruby-2.0.0-p598/gems/database_cleaner-1.3.0/lib/database_cleaner/base.rb:147:in
`autodetect': No known ORM was detected! Is ActiveRecord, DataMapper,
Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm
loaded? (DatabaseCleaner::NoORMDetected)
spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.use_transactional_fixtures = false
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.expect_with :rspec do |c|
c.syntax = [:should, :expect]
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.color = true
Selenium::Application.reload_routes!
end
database_cleaner.rb
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before :each do
DatabaseCleaner.start
end
config.after :each do
DatabaseCleaner.clean
end
end
I had same issue on controller_spec.
'autodetect': No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded? (DatabaseCleaner::NoORMDetected)
I resolved by requiring rails_helper file on controller spec.
require 'rails_helper'
In rails_helper.rb require file 'database_cleaner'.
require 'database_cleaner'
I had this issue (on Rails 5.1) and the reason was that I had
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
in my spec_helper.rb.
I put require 'database_cleaner in spec_helper.rb as well.
So eventually I moved both things to rails_helper.rb and that fixed the issue for me.
Use my setup, seems to work fine for RDBMS (checked on MySQL and Postgres), put it into your database_cleaner.rb:
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
# this may be needed for Capybara tests and for testing after_commit hooks
config.before(:each, strategy: :truncation) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
If you want to use truncation strategy, just use
describe 'something', strategy: :truncation or it 'something', strategy: truncation.

Can't use named routes in my rspec feautre tests.

I have the tests that looks like the following:
require "rails_helper"
feature "Ordering units management" do
scenario "Creating new ordering unit" do
visit new_ordering_units_path
fill_in I18n.t('activerecord.attributes.ordering_unit.name'), with: "Ordering unit name"
click_button I18n.t('submit')
expect(page).to have_text(I18n.t('ordering_units.created'))
expect(page).to have_content("Ordering unit name")
end
end
When I run it I have the following error message:
undefined local variable or method `new_ordering_units_path' for #<RSpec::ExampleGroups::OrderingUnitsManagement:0x00000004962810>
My spec_helper:
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.filter_run :focus
config.run_all_when_everything_filtered = true
if config.files_to_run.one?
config.default_formatter = 'doc'
end
config.profile_examples = 10
config.order = :random
Kernel.srand config.seed do
end
end
My rails helper:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'capybara'
require 'rspec/rails'
require 'capybara/rspec'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Capybara::DSL
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
config.infer_spec_type_from_file_location!
end
I can't find solution for this. Could anyone tell me why this ain't working?
It should be called new_ordering_unit_path (singular). Type rake routes in your console to see all routes in effect.

Object is created. (RoR, Database Cleaner, Factory Girl)

I'm a beginner in Rails, help me, I do not understand what the problem is. It seems like something else is written before(:each)...
And How can I write a test in test_spac.rb in which no Card object is created. That the method before(:each) did not work for this test.
This is work:
test_spec.rb:
require 'rails_helper'
describe "Index Page" do
before(:each) do
#card = FactoryGirl.create(:card)
DatabaseCleaner.start
end
it "shows error messages if translation is wrong" do
visit root_path
fill_in 'translated_text', with: 'wqww'
click_on 'Проверить'
expect(page).to have_content('Неверно!')
end
after(:each) do
DatabaseCleaner.clean
end
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'
require 'capybara/rspec'
require 'capybara/poltergeist'
require 'database_cleaner'
Capybara.javascript_driver = :poltergeist
Capybara.default_driver = :poltergeist
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 = false
config.before(:each) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each, type: :features) do
DatabaseCleaner.strategy = :truncation
end
config.infer_spec_type_from_file_location!
end
This is NOT Work:
test_spec.rb:
require 'rails_helper'
describe "Index Page" do
before(:each) do
#card = FactoryGirl.create(:card)
end
it "shows error messages if translation is wrong" do
visit root_path
fill_in 'translated_text', with: 'wqww'
click_on 'Проверить'
expect(page).to have_content('Неверно!')
end
end
rails_helper.rb:
config.use_transactional_fixtures = false
config.before(:each) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each, type: :features) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
Problem solved:
I make this: (test_spec.rb)
require 'rails_helper'
describe "Index Page" do
it "check behavior then translation is wrong" do
FactoryGirl.create(:card) #################
visit root_path
fill_in 'translated_text', with: 'some text'
click_on 'Проверить'
expect(page).to have_content('Неверно!')
end
it "check page then object is not created" do
visit root_path
expect(page).to have_content('Непросмотренных')
end
end
Could you post what you mean by not working? Are you getting any error messages?
By the way, why do you have config.use_transactional_fixtures disabled? If you stick to using before(:each) hooks, RSpec will reset the database for you and you shouldn't need to use the DatabaseCleaner.

'Factory not registered' error in Rails 4

I am getting a 'Factory not registered' error using Factory Girl in Rails 4. Here is my factory definition for a simple Devise user in spec/factories/user.rb:
FactoryGirl.define do
factory :user do
email 'test#example.com'
password 'foobar'
password_confirmation 'foobar'
end
end
Here is the spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'email_spec'
require 'rspec/autorun'
require 'factory_girl'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Devise::TestHelpers, :type => :controller
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
And in the gem file:
gem 'factory_girl_rails', :require => false
And here is the test in spec/models/user_spec.rb:
require 'spec_helper'
describe User do
it "has a valid factory" do
FactoryGirl.build(:user).should be_valid
end
end
This fails, saying 'Factory not registered: user'. I've tried moving the Factory definition to spec/factories.rb, as well as using just factory_girl instead of factory_girl_rails, with no luck. Any suggestions would be greatly appreciated.
You're requiring factory_girl instead of factory_girl_rails. Edit the equivalent line in your spec_helper.rb to this:
require 'factory_girl_rails'

Resources