How can i run "rails routes --expanded" exclusively in Rails6 Test Environment? - testing

Below details are in reference of Rails 6.
I have created category controller using scaffold in the test environment. I want to crosscheck all the routes and its URI using rails routes --expanded for test environment.
How can i do that?
As to run that command directly in terminal will give routes of development environment.
I have already checked rails console -e test but its exclusively for irb of test env.

You can use RAILS_ENV=environment before the command like this:
RAILS_ENV=test rails routes --expanded
This will output all routes in your test environment.

Related

Rails Capybara run a specific file (test)

Currently I have a huge integration test in a single file in my Rails 3 app in spec/main_spec.rb, and to run this test I simply do 'rake spec'. I want to begin to break this test up into differnt files. So lets say I create another test (file) called spec/another_spec.rb. How can I run this specific file? When I do...
bundle exec rake spec/another_spec.rb
Nother seems to happen...I don't get any errors, but I don't any feedback telling me whats passed or failed.
bundle exec rspec spec/another_spec.rb
or
rspec spec/another_spec.rb

Running Capybara tests at a different url when on Travis CI

I have some integration tests written in Capybara which I'm running on Travis. In the tests I hit a hardcoded url (given by Pow and symlinks) with the visit method. This of course does not work well on Travis. What I need to do is to somehow distinguish environments. So when the tests run on Travis they are hitting a different url like localhost:5000 for example. I put that in my .Travis.yml file that it will start a rails server in the background which works fine. The question is how do I make the tests use that url instead?
My config looks something like this:
language: ruby
rvm:
- 1.9.3
before_script:
- RAILS_ENV=test bundle exec rake db:create
- "bundle exec rails server -p 5000 &"
- "sleep 5" # Wait for the server to start up"
script: bundle exec rspec
I'm using PhantomJS through poltegeist gem. I'm thinking if I could somehow use the Travis env var. Anyone got any suggestions on this?
Thanks!
I'm not very familiar with Travis CI but I don't see any reason you need to hardcode the server name into url.
Instead of
visit 'http://localhost:5000/about'
You can use
visit '/about'
Or better
visit about_path
Less dependency is always better. I suggest your tweaking the tests.
Travis CI sets several environment variables for you that you can use. I think TRAVIS=true may be of interest:
Capybara.app_host = if ENV['TRAVIS']
'localhost:5000'
else
'http://www.example.com'
end

How to load environment variables in the Rails console?

I think this is a little, easy question!
I'm using .env file to keep all my environment variables, and i'm using foreman.
Unfortunately, these environment variables are not being loaded when running rails console rails c so, i'm now loading them manually after running the console, which is not the best way.
I'd like to know if there any better way for that.
About a year ago, the "run" command was added to foreman
ref: https://github.com/ddollar/foreman/pull/121
You can use it as follow:
foreman run rails console
or
foreman run rake db:migrate
rails does not know about the environmental variables specified in .env file as it is specific to foreman. You need to set the environment explicitly before invoking rails console. Have a look at this question.
I personnaly use dotenv in development and testing environements. With this approach, you don't have to prefix your commands, just call the initializer in your config/application.rb :
Bundler.require(*Rails.groups)
Dotenv::Railtie.load
HOSTNAME = ENV['HOSTNAME']

How to manage migrations for a rails engine + dummy app

I just joined a project developing a rails engine, that also has a dummy app for testing.
foo/
foo/spec/dummy/
There are identical migrations in
foo/db/migrate/
foo/spec/dummy/db/migrate/
If I rake db:migrate from the dummy app, all is well. If I do the same from the engine (current directory = foo) I get an error about multiple migrations with the same name.
Q1) Are the Rakefiles borked? (should db:migrate recurse down to the dummy app?)
Q2) Should the migrations only be in one directory? If so, which one?
We are using Rails 3.2.9, ruby 1.9.3p194.
Question 1
The Rakefile should have an entry to account for the spec/dummy app. For example,
Bundler::GemHelper.install_tasks
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'
Here's more detailed example rakefile, https://github.com/twinge/questionnaire_engine/blob/engine2/Rakefile
Question 2
IMO, the migrations should only exist on the foo/db/migrate folder, and not the foo/spec/dummy/db/migrate. In fact, I don't version control the dummy's db/migrate or the db/schema.
Why? I use the dummy app the make sure a full on install of my engine works 100%. Therefore, if I version controlled the foo/spec/dummy db state, I would be testing as if there was a previous install.
Example Engine
https://github.com/twinge/questionnaire_engine/tree/engine2
For testing the dummy app, you can run your engine migrations for the test ENV using the following:
RAILS_ENV=test rake db:migrate

Register a script to use with the rails command, like `rails mycommand`

The rails command-line command provides a couple of commands, like rails generate, rails console etc. Now I'd like write a gem which registers my own command for use with rails mycommand.
Is this possible?
If so, any guides on how to do that?
NB: This is for rails 3+
regards, apeiros
Haven't done it, but here are some leads. In your Rails app, the script directory holds a file called 'rails' that has this line
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
It then requires this Rails file: https://github.com/rails/rails/blob/master/railties/lib/rails/commands.rb