Launch rails in a test environment

I am trying to load rails in a test environment using ruby ​​script. I tried a little work and found this recommendation:

require "../../config/environment"
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'test'

This is similar to loading my environment, but my database is still in use. Am I doing something wrong?

Here is my database.yml file ... however I don't think this is a problem

development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: BrianSite_development
  pool: 5
  username: root
  password: dev
  host: localhost

# 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:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: BrianSite_test
  pool: 5
  username: root
  password: dev
  host: localhost

production:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: BrianSite_production
  pool: 5
  username: root
  password: dev
  host: localhost

I can not use

ruby script/server -e test

because I'm trying to run ruby ​​code after loading rails. More specifically, what I'm trying to do: run the .sql script database, load the rails and then run the automated tests. Everything seems to be working fine, but for some reason, the rails seem to be loading into the development environment instead of the test environment.

Here is a shortened version of the code I'm trying to run:

system "execute mysql script here"

require "../../config/environment"
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'test'

describe Blog do
  it "should be initialized successfully" do
    blog = Blog.new
  end
end

, (, ..), .

.

UPDATE:

. . :

require"spec"
require "spec/rake/spectask"
RAILS_ENV = 'test'

namespace :run_all_tests do
  desc "Run all of your tests"

  puts "Reseting test database..."
  system "mysql --user=root --password=dev < C:\\Brian\\Work\\Personal\\BrianSite\\database\\BrianSite_test_CreateScript.sql"
  puts "Filling database tables with test data..."
  system "mysql --user=root --password=dev < C:\\Brian\\Work\\Personal\\BrianSite\\database\\Fill_Test_Tables.sql"

  puts "Starting rails test environment..."
  task :run => :environment do
    puts "RAILS_ENV is #{RAILS_ENV}"
    require "spec/models/blog_spec.rb"
  end
end

, spec/models/blog_spec.rb, , , ...

.

+3
3

, , Rake, ? rake -T test, , .

- , Rake. - lib/tasks:

namespace :custom_tests do
  desc "Run my custom tests"
  task :run => :environment do
    puts "RAILS_ENV is #{RAILS_ENV}"
    system "execute mysql script here"
    # Do whatever you need to do
  end
end

=> :environment . : RAILS_ENV=test rake custom_tests:run

+6

. Database.

$rails s -e test
+27

to start the test environment, you must run script / server with the parameter -e:

ruby script/server -e test

and in your config / database.yml there should be a defenition of the test env database, that is:

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000
+9
source

Source: https://habr.com/ru/post/1745017/


All Articles