Rails Setting up unique integration environment - ruby-on-rails

I am trying to setup a unique environment (but effectively a new development environment so that various global parameters can be different). I've followed lots of examples to create a new environment (I used my development config as the starting point).
My new environment is singleserverintegration.
a new environment.rb
added new logic to initializers/additional.rb (elsif Rails.env.eql?("singleserverintegration"))
added entries to database.yml
BUT when-ever i attempt to setup the environment
RAILS_ENV="singleserverintegration" && rake db:drop && rake db:create && rake db:migrate
I get a
rake aborted!
uninitialized constant Capybara
why is it pulling out test configuration (which is where capybara is used as part of rspec) [i've noticed that additional.rb has capybara config reguardless of the environment, but it never complains when i run rake / db commands for my dev environment. why would it complain now?]
what am i missing - guidance appreciated for a relative newbie...
thanks
Ben
bottom line of additional.rb is
Capybara.server_port = 8066
but, this does not cause an issue when i setup development environments!?

try adding your new env to the capybara gem as well
Ex:
group :test, :development, :singleserverintegration do
gem 'capybara'
end

Related

Rspec keeps using development database

I have different databases for development and for test in database.yml.
I have ENV['RAILS_ENV'] = 'test' in start of both files rails_helper.rb and spec_helper.rb, generated with rspec initialisation command.
Also, I tried Rails.env = 'test' without success.
I run rspec as RAILS_ENV=test rake spec
I don't have ENV['RAILS_ENV'] setting or Rails.env setting anywhere in initialisation scripts.
However, Rspec keeps using development database instead of testing one.
I have checked a lot of similar questions around the internet and no answer helped.
Any solutions?
What version of rails are you using? Try rake db:test:prepare. In Rails 4+, It's deprecated but still works. It seems like in rails 4.1+, migrations are automatically checked for, and running db:schema:load will do the trick.

How do you set the RAILS_ENV variable when using RVM?

I have a rake task in my rails app that has a different absolute filepath compared between prod and dev. I have a different database username/pass that differs between prod and dev. I can't seem to get the RAILS_ENV variable to be set within RVM in order to tell each server what their role is. Is there a way to do this?
My workaround has been to do the following for all rake tasks:
RAILS_ENV=development rake routes
RAILS_ENV=production rake db:migrate
Been having similar problems (not with RVM though). One way we did it was to assign ENV["RAILS_ENV"] through envrionment.rb
It's not the actual answer, but it will give you some ideas:
#config/environment.rb
ENV["RAILS_ENV"] = "staging"
You may wish to look at assigning environment vars in RVM here: http://matthew.mceachen.us/blog/howto-make-system-wide-rvm-installations-work-with-cron-monit-delayed_job-and-passenger-1021.html

How to debug "database does not exist" error in test suite?

I recently set up a Rails dev environment on a new machine. I'm now trying to run an app's test suite, but keep getting database errors.
Failure/Error: user = FactoryGirl.create(:user)
PG::ConnectionBad:
FATAL: database "test-db" does not exist
My database.yml file is set up correctly. I've run psql -l and test-db does in fact exist, and this is confirmed by running rake db:create where I get an 'already exists' message.
I've run rake db:migrate and rake:test:prepare, but the error still remains.
My test set up is as follows
group :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'capybara'
gem 'guard-rspec'
gem 'spork-rails'
gem 'guard-spork'
end
I'm having no trouble with the development database.
Why am I getting a db does not exist error, and what can I do to debug this?
Try running this first:
rake db:create
I'll add the solution that helped me (should someone come across this). I had to run:
bundle exec rake db:setup
This re-creates a database and runs any migrations.
After a lot of trial and error, I finally managed to get the test database working. Leaving the following here in case anyone else runs into this issue.
The issue was either an incorrect username in database.yml (While I did change these values, I don't think this was the cause as both development and test were using the same username previously).
Or a small spelling mistake in a config var set up elsewhere ENV["DATABASE_URL"] = "postgres://localhost/test_db". This should have been test-db.

How to write ruby script that uses the production environment instead of development?

I have writen a ruby script that requires config/environment.rb so I can use all the models of my rails application in the script but the problem is that I want to use the production environment instead of the develoment environment which seems to be the default behavior.
Im using Rails 3.1.1 and Ruby 1.9.2
How can I run the script with the production environment?
Your script will take the environment variable RAILS_ENV as the environment to use.
I'd be very wary of overriding that in the script as it may cause much confusion if you try and run your script in another environment - e.g. staging - and it starts trying to access production databases etc.
So do either:
RAILS_ENV=production ./script/my-awesome-script
or
export RAILS_ENV=production
./script/my-awesome-script
Generally speaking; when I log into a production Rails environment I'd be changing the environment straight away if I haven't configured it to be "production" by default.
I think you want either Rails.env = 'production' or ENV['RAILS_ENV'] = 'production' in your script.
#davidb, I am not sure what u want also not as good on rails yet but may be you can use the seed.rb (i.e seed functionality) for achieving what you want if this script is run only once or sometimes as we can specify the environment while running seed
rake db:seed RAILS_ENV=production

How to run Rails console in the test environment and load test_helper.rb?

The background: I'm having some problems with Thoughtbot's "Factory Girl" gem, with is used to create objects to use in unit and other tests. I'd like to go to the console and run different Factory Girl calls to check out what's happening. For example, I'd like to go in there are do...
>> Factory(:user).inspect
I know that you can run the console in different environments...
$ script/console RAILS_ENV=test
But when I do that, Factory class is not available. It looks as though test_helper.rb is not getting loaded.
I tried various require calls including one with the absolute path to test_helper.rb but they fail similarly to this:
$ script/console RAILS_ENV=test
>> require '/Users/ethan/project/contactdb/test/test_helper.rb'
Errno::ENOENT: No such file or directory -
/Users/ethan/project/contactdb/config/environments/RAILS_ENV=test.rb
Grr. Argh.
For Rails < 3.0
Run script/console --help. You'll notice that the syntax is script/console [environment], which in your case is script/console test.
I'm not sure if you have to require the test helper or if the test environment does that for you, but with that command you should at least be able to boot successfully into the test env.
As a sidenote: It is indeed kind of odd that the various binaries in script/ has different ways of setting the rails environment.
For Rails 3 and 4
Run rails c test. Prepend bundle exec if you need this for the current app environment.
For Rails 5 and 6
Run rails console -e test.
In Rails 3, just do rails console test or rails console production or rails console development (which is the default).
For Rails 5.2.0: "Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead."
rails c -e test
script/console test
Should be all you need.
You can specify the environment in which the console command should operate.
rails c [environment]
Examples
1) For Staging
rails c staging
2) For Production
rails c production
For source & detailed description: The Rails Command Line
David Smith is correct, just do
script/console test
The help command will show why this works:
$ script/console -h
Usage: console [environment] [options]
-s, --sandbox Rollback database modifications on exit.
--irb=[irb] Invoke a different irb.
--debugger Enable ruby-debugging for the console.
It's the [environment] bit.
I share the asker's pain. There are really three separate questions here, some of which are addressed, some not:
How do you start the console in the test environment?
For recent Rails versions, bundle exec rails c test, or alternative syntaxes for that.
How do you ensure that test/test_helper.rb is loaded into that console session?
Something like require './test/test_helper' ought to do it.
For me, this returns true, indicating that it was not already loaded when I started the console. If that statement returns false, then you just wasted a few keystrokes, but you're still good to go.
Once test_helper is loaded, how do you call the methods defined in it?
In a typical test_helper, the custom methods are typically defined as instance methods of ActiveSupport::TestCase. So if you want to call one of them, you need an instance of that class. By trial and error, ActiveSupport::TestCase.new has one required parameter, so...pass it something.
If your test_helper has a method called create_user, you could invoke it this way:
ActiveSupport::TestCase.new("no idea what this is for").create_user
Make sure you installed the GEM and you added the following line either in your environment.rb or test.rb file.
config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"
Test Env
rails console test # or just rails c test
Developement Env
rails console # or just rails c
Command to run rails console test environment is
rails c -e test
or
RAILS_ENV=test rails c
if you are facing problem something like
ActiveRecord::StatementInvalid:
Mysql2::Error: Table 'DB_test.users' doesn't exist: SHOW FULL FIELDS FROM `users`
then you should first prepare your test DB by running
bundle exec rake db:test:prepare

Resources