RSpec slow when Rails use another Rails DB - ruby-on-rails

I got a strange situation on RSpec when Rails testing.
Normaly by using spring RSpec file taken time is very fast.
But, when using another Rails DB RSpec file taken time is 2 or 3 seconds.
My Rails app use another Rails app database.yml to use the same db.
Why is RSpec so slow? and How do I make RSpec fast?
I confirmed this is happened at Rails4 and Rails5.
Environment
DB PostgreSQL 9.4.5
This is my code.
class Test
def self.test
'test'
end
end
This is my test code.
require 'rails_helper'
RSpec.describe Test do
describe "Test.test" do
it "test" do
expect(Test.test).to eq 'test'
end
end
end
This is my database.yml. Two rails app use same yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: qserver_development
test:
<<: *default
database: qserver_test

Related

Launch rspec tests with sqlite :memory: - "migrations are pending"

I want to run tests using rspec on my ruby on rails app using sqlite with the memory database. However, everytime I launch rspec it tells me that migrations are pending, event if I run the migrations before hand. Is there a way to do the migrations everytime before I run the tests ?
Here's my database configuration
test:
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
database: ":memory:"
You need to load schema in your tests instead of relying on migrations.
As advised in this blogpost replace
ActiveRecord::Migration.maintain_test_schema!
with
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.verbose = false
load "#{Rails.root.to_s}/db/schema.rb"

ymRuby on rails: Running specs for models

I'm new to rspec framework, and I have a rails Application that I wish to use Rspec to write unit tests for it.
I followed the instructions in https://github.com/rspec/rspec-rails to install the rspec-rails gem.
I've written a very simple spec file that requires 'rails_helper' and have some pending tests only.
When I try to run this file using command bundle exec rspec I get this error
An error occurred while loading ./spec/models/invoice_spec.rb.
Failure/Error: include DeviseTokenAuth::Concerns::User
Mysql2::Error:
Access denied for user 'root'#'localhost' (using password: NO)
How do I resolve this so that I can run my specs?
Edit : database.yml file
default: &default
adapter: mysql2
encoding: utf8
username: <%= Rails.application.secrets[:mysql_db_user] %>
password: <%= Rails.application.secrets[:mysql_db_password] %>
pool: 5
timeout: 5000
development:
<<: *default
database: tomav_dev
test:
<<: *default
database: tomav_test

rake corrupts development database

When I run rake to run specific/all test(s), it corrupts development database.
I verified that db for each environment is different.
database.yml
postgre_common_config: &postgre_common_config
adapter: postgresql
encoding: unicode
pool: 5
username: postgres # if using default PostgreSQL user then the value should be: postgres
password: postgres
host: localhost
development:
database: mag_development
<<: *postgre_common_config
test:
database: mag_test
<<: *postgre_common_config
production:
database: mag_production
<<: *postgre_common_config
test_helper.rb
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
fixtures :all
end
This is really annoying as it takes lot of time to prepare data to test specific feature manually. Please help.

how to trigger a rake:db:prepare that it also migrates an external database connection

I'm currently trying to perform some tests on a rails app that has two database connections.
As of course I wouldn't like to delete the 2nd database each time I perform a test, I have set up a mechanism to connect to another 2nd-DB if the environment is "test" (see below).
My question: How could I tell rake test to also perform a rake db:test:prepare on the 2nd DB?
This query already touched my problem but couldn't help me completely.
Some code to explain:
My ActiveRecord-Classes that connect to the 2nd db inherit from the following class InformixConnect:
class InformixConnect < ActiveRecord::Base
self.abstract_class = true
case Rails.env
when 'production', 'development'
establish_connection :development_informix
when 'test'
establish_connection :test_informix_dummy
else
raise "Please specify a correct informix Environment."
end
end
like
class RenewalNotify < InformixConnect
set_table_name :renewal_notify
set_primary_key :renewal_notify_id
end
(yes, I know... The schema does not follow the rails conventions. It's a legacy one)
database.yml contains
...
development:
adapter: postgresql
database: rails_development
host: 127.0.0.1
reconnect: true
username: rails
password: guessone
...
development_informix:
adapter: informix
database: SOMETHING
username: yyy
password: yyy
test_informix_dummy:
adapter: sqlite3
database: db/test_informix_dummy.sqlite3
...
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test: &TEST
adapter: sqlite3
database: db/test.db
verbosity: silent
timeout: 5000
So I'd need to setup the "test_informix_dummy" db each time I perform a rake test

mongoid with rails - Database should be a Mongo::DB, not NilClass"

Greetings
I am trying to get Mongoid to work with my Rails app and I am getting
an error: "Mongoid::Errors::InvalidDatabase in 'Shipment bol should be
unique' Database should be a Mongo::DB, not NilClass"
I have created the mongoid.yml file in my config directory and have mongodb running as a daemon. The config file is like so:
defaults: &defaults
host: localhost
development:
<<: *defaults
database: ship-it-development
test:
<<: *defaults
database: ship-it-test
production:
<<: *defaults
host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
All of my specs fail with the above error. I am using rails 2.3.8.
Anyone have ideas?
Like explain on question : How can i generate mongoid.yml config in Rail 2.3.5?
The mongoid.yml doesn't works with Rails 2.3.x. It's load automatic only with Rails 3.
You need add an initializer with loading your file and use it to define your database.
By example you can add that in an initializer.
mongoid_conf = YAML::load_file(Rails.root.join('config/mongoid.yml'))[Rails.env]
Mongoid.configure do |config|
config.master = Mongo::Connection.new(mongoid_conf['host'],
mongoid_conf['port']).db(mongoid_conf['database'])
end
Also if your writing your own non rails script and you initialize your models first then you will get this error.
You need to configure the database before initializing the model.
I hit this when writing a gem that used mongoid internally
Test cases hit it as well so put the Mongoid.configure section in your test/helper.rb

Resources