Rake Cucumber and Rake Spec Always Use "Develop" Environment

Rake stats and Cucumber

RSpec creates a lib/tasks/rspec.rake file. And redefines the stats directories inside of it.

At the line 108 of that file, you'll see :

# Setup specs for stats
task :statsetup do
require 'code_statistics'
::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models')
::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views')
....
end

You just need to add your cucumber features directory there, right before the end of the task.

#
# Adding Cucumber features to the stats
#
::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')

Bundle exec rake spec and custom rake tasks

I had some similar issues with my DB getting wiped whenever I would run the unit tests manually with bundle exec rspec spec, and it turned out to be that it was using the development environment even though I was setting it manually in spec_helper.rb with ENV["RAILS_ENV"] = 'test'.

Now I just explicitly specify RAILS_ENV=test bundle exec rspec spec or you can put that in a dotests.sh script.

See these threads:

rake cucumber and rake spec always use "develop" environment

Rails 4, New App: Why do tests run in development environment? (I tried the Rails.env = 'test' thing and it didn't work for me, but give it a shot)

(apparently) identical tests for two rake tasks; only one passes

Nutshell: Change your before to a before :all (instead of :each).

Or: Pass an empty array as a third parameter to rake_require.

Rake.application.rake_require 'lib/tasks/demo_tasks', 
[Rails.root.to_s],
[]

Details

def rake_require(file_name, paths=$LOAD_PATH, loaded=$")
  fn = file_name + ".rake"
  return false if loaded.include?(fn)
...

$" is a Ruby special variable that holds an array of modules loaded by require.

If you don't pass the optional parameter, rake_require will use that array of modules loaded by Ruby. This means the module won't be loaded again: Ruby knows the module was loaded, rake checks to see what Ruby knows, and it's a new rake instance for each test.

Switching to before :all worked because it meant the let block only ran once: one rake instance, one module load, everybody is happy.

All this said, why reload the rake environment twice anyway? Your goal is to test your tasks, which doesn't require a fresh rake context for every spec.

You could eliminate the local altogether at the cost of some minor verbosity in each spec:

describe "test tasks" do
before :all do
Rake.application = Rake::Application.new
Rake.application.rake_require 'lib/tasks/demo_tasks', [Rails.root.to_s]
Rake::Task.define_task :environment
end

describe "demo:test" do
it "runs" do
Rake::Task["demo:test"].invoke
end
end
end

You could define an instance variable in the before block to avoid the Rake::Task reference:

before :all do
@rake = Rake::Application.new
Rake.application = @rake
Rake.application.rake_require 'lib/tasks/demo_tasks', [Rails.root.to_s]
Rake::Task.define_task :environment
end

describe "demo:test" do
it "runs" do
@rake["demo:test"].invoke

IMO, less desirable for a number of reasons. Here's a summary I agree with.

Cucumber and RSpec testing with zeus: Postgres is being accessed by other users

Inspired by this answer, we created the following database.rake file. Where the original answer worked only for PostgreSQL 9.1, this one is modified to work for PostgreSQL 9.2 as well. The mechanism is not the prettiest: when the 9.1 command fails, it simply executes the 9.2 command. But the most important thing: it works!

#{Rails.root}/lib/tasks/databases.rake
# monkey patch ActiveRecord to avoid There are n other session(s) using the database.
def drop_database(config)
case config['adapter']
when /mysql/
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.drop_database config['database']
when /sqlite/
require 'pathname'
path = Pathname.new(config['database'])
file = path.absolute? ? path.to_s : File.join(Rails.root, path)

FileUtils.rm(file)
when /postgresql/
begin
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by procpid;").each do |x|
if config['database'] == x['datname'] && x['current_query'] =~ /<IDLE>/
ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['procpid']})")
end
end
ActiveRecord::Base.connection.drop_database config['database']
rescue # in PG 9.2 column procpid was renamed pid and the query status is checked not using 'current_query' but using 'state'
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by pid;").each do |x|
if config['database'] == x['datname'] && x['state'] =~ /idle/
ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['pid']})")
end
end
ActiveRecord::Base.connection.drop_database config['database']
end
end
end

Cucumber and RSpec testing with zeus: Postgres is being accessed by other users

Inspired by this answer, we created the following database.rake file. Where the original answer worked only for PostgreSQL 9.1, this one is modified to work for PostgreSQL 9.2 as well. The mechanism is not the prettiest: when the 9.1 command fails, it simply executes the 9.2 command. But the most important thing: it works!

#{Rails.root}/lib/tasks/databases.rake
# monkey patch ActiveRecord to avoid There are n other session(s) using the database.
def drop_database(config)
case config['adapter']
when /mysql/
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.drop_database config['database']
when /sqlite/
require 'pathname'
path = Pathname.new(config['database'])
file = path.absolute? ? path.to_s : File.join(Rails.root, path)

FileUtils.rm(file)
when /postgresql/
begin
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by procpid;").each do |x|
if config['database'] == x['datname'] && x['current_query'] =~ /<IDLE>/
ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['procpid']})")
end
end
ActiveRecord::Base.connection.drop_database config['database']
rescue # in PG 9.2 column procpid was renamed pid and the query status is checked not using 'current_query' but using 'state'
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by pid;").each do |x|
if config['database'] == x['datname'] && x['state'] =~ /idle/
ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['pid']})")
end
end
ActiveRecord::Base.connection.drop_database config['database']
end
end
end

Environment data not found in the schema. with database-cleaner when calling cucumber repeatedly on Rails 6

Turns out this problem was specific to database_cleaner 1.7.

A recent upgrade to 1.8.3 made this error stop happening.

(associated Github issue)

rake db:migrate and rake db:create both work on test database, not development database

I had the exact same problem starting last night. No idea what might have caused this, but finally found a solution that worked. Inside the configure block in config/environments/develop.rb, I added:

Rails.env = 'development'

I hope that works for you too



Related Topics



Leave a reply



Submit