Why I am not able to run rails tests - ruby-on-rails

This is what I did.
> git clone git://github.com/rails/rails.git
> cd rails
> cd railties
> rake
And I got following error.
(in /Users/dorelal/dev/scratch/rails/railties)
./test/isolation/abstract_unit.rb:236:in `initialize': No such file or directory - /Users/dorelal/dev/scratch/rails/railties/tmp/app_template/config/boot.rb (Errno::ENOENT)
from ./test/isolation/abstract_unit.rb:236:in `open'
from ./test/isolation/abstract_unit.rb:236
from ./test/isolation/abstract_unit.rb:222:in `initialize'
from ./test/isolation/abstract_unit.rb:222:in `new'
from ./test/isolation/abstract_unit.rb:222
from test/application/configuration_test.rb:1:in `require'
from test/application/configuration_test.rb:1
rake aborted!
I checked ~/railties/tmp and this directory is empty.
I know rails is not broken. So what am I missing?

You need to run bundle install after you cd rails. That will install all the gems required by your new Rails repo, including minitest (test-unit at the time of your question.)
After you run bundle install, you will be able to run the rails tests by doing a cd into the component directory and then either running the entire test suite for that component, or specifying a single test file, as follows (using actionpack as an example):
To run all tests for actionpack:
cd actionpack
TEST_DIR=generators bundle exec rake test
To run the tests for actionpack test/dispatch/rack_test.rb:
cd actionpack
bundle exec ruby -Itest test/dispatch/rack_test.rb
To run the entire test suite for all of rails:
cd rails
bundle exec rake test
A Necromancer cannot resist such a question - so old, yet so relevant, so cleanly asked, so not documented properly.

Related

Rails: Could not find a JavaScript runtime while trying to run tests in rubymine

I have a rails 6.1 app with an rspec test suite running on ubuntu 20.04 and i use rubymine 2022.1.3 as an IDE when i try to run the tests from the Run configuration like spec for example or when i click on the icon next to the test definition i get this error
/bin/bash -c "env RBENV_VERSION=3.0.3 /home/alex/.rbenv/libexec/rbenv exec ruby -x /var/www/project/bin/bundle exec ruby /var/www/project/bin/rails spec"
Testing started at 5:17 μ.μ. ...
rails aborted!
ExecJS::RuntimeUnavailable: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
/var/www/project/config/application.rb:17:in `<top (required)>'
/var/www/project/Rakefile:9:in `require'
/var/www/project/Rakefile:9:in `<top (required)>'
/var/www/project/bin/rails:4:in `require'
/var/www/project/bin/rails:4:in `<main>'
(See full trace by running task with --trace)
i get the same error when i try to run the debugger or tests with coverage and pretty much everything that goes through Run anything
/bin/bash --login -c "env RBENV_VERSION=3.0.3 /home/alex/.rbenv/libexec/rbenv exec rails g controller home"
but not
Tools > bundle > install
/bin/bash -c "env RBENV_VERSION=3.0.3 /home/alex/.rbenv/libexec/rbenv exec bundle install"
the app is running just fine locally and deployments are all fine. i even tried installing the execjs, mini_racer and libv8-node gems but i keep getting the same error
if i open another project with rails and rspec the Run configurations run just fine
i noticed that the command that rubymine uses to run the tests is slightly different on project with the working Run configurations
/bin/bash -c "env RBENV_VERSION=3.0.3 /home/alex/.rbenv/libexec/rbenv exec ruby -x /var/www/password_manager/bin/bundle exec ruby /var/www/password_manager/bin/spring rails spec"
/bin/bash -c "env RBENV_VERSION=3.0.3 /home/alex/.rbenv/libexec/rbenv exec ruby -x /var/www/project/bin/bundle exec ruby /var/www/project/bin/rails spec"
spring is not running
node version: /home/alex/.nvm/versions/node/v16.13.0/bin/node
has anyone encountered this before?
is there a way to change the command that rubymine uses?

Migrating database fails after upgrading to Ruby 2.3.0

I've updated to Ruby 2.3.0 and I'm having some issues when creating a new Rails app. After creating a simple new test app and scaffolding a resource, when trying to execute rake db:migrate I'm getting the following load error:
MacBook-Pro:log medright1$ rake db:migrate
/Users/medright1/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/rake-10.4.2/bin/rake:31:in `require': cannot load such file -- rake (LoadError)
from /Users/medright1/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/rake-10.4.2/bin/rake:31:in `<top (required)>'
from /Users/medright1/.rvm/gems/ruby-2.3.0/bin/rake:23:in `load'
from /Users/medright1/.rvm/gems/ruby-2.3.0/bin/rake:23:in `<main>'
from /Users/medright1/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/medright1/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
MacBook-Pro:log medright1$
Any help sorting this would be great!
If you haven't done it yet, make sure you have all the dependencies installed.
$ bundle
or
$ bundle install
It's likely you don't have rake installed in the global RVM gemset. In any case, given you are within a Rails project, you should use bundler to execute the command.
$ bundle exec rake db:migrate
Otherwise, make sure to install rake globally
$ rvm gemset use global
$ gem install rake
However, the correct way is to execute the command via Bundler.

cannot seem to update the rails executable in bin/

I have a rails project where I use bin/rails instead of just calling rails its self. upon upgrading to rails 4.2, I did:
rm -rf .bundle/gems
bundle
When everything finished, I did bin/rails
$ bin/rails
Looks like your app's ./bin/rails is a stub that was generated by Bundler.
In Rails 4, your app's bin/ directory contains executables that are versioned
like any other source code, rather than stubs that are generated on demand.
Here's how to upgrade:
bundle config --delete bin # Turn off Bundler's stub generator
rake rails:update:bin # Use the new Rails 4 executables
git add bin # Add bin/ to source control
You may need to remove bin/ from your .gitignore as well.
When you install a gem whose executable you want to use in your app,
generate it and add it to source control:
bundle binstubs some-gem-name
git add bin/new-executable
/Users/Adam/Documents/Rails-Projects/Libraries-and-Helpers/Xaaron/.bundle/gems/gems/railties-4.2.0/lib/rails/app_rails_loader.rb:44:in `require': cannot load such file -- /Users/Adam/Documents/Rails-Projects/Libraries-and-Helpers/Xaaron/config/boot (LoadError)
from /Users/Adam/Documents/Rails-Projects/Libraries-and-Helpers/Xaaron/.bundle/gems/gems/railties-4.2.0/lib/rails/app_rails_loader.rb:44:in `block in exec_app_rails'
from /Users/Adam/Documents/Rails-Projects/Libraries-and-Helpers/Xaaron/.bundle/gems/gems/railties-4.2.0/lib/rails/app_rails_loader.rb:34:in `loop'
from /Users/Adam/Documents/Rails-Projects/Libraries-and-Helpers/Xaaron/.bundle/gems/gems/railties-4.2.0/lib/rails/app_rails_loader.rb:34:in `exec_app_rails'
from /Users/Adam/Documents/Rails-Projects/Libraries-and-Helpers/Xaaron/.bundle/gems/gems/railties-4.2.0/lib/rails/cli.rb:5:in `<top (required)>'
from /Users/Adam/Documents/Rails-Projects/Libraries-and-Helpers/Xaaron/.bundle/gems/gems/railties-4.2.0/bin/rails:9:in `require'
from /Users/Adam/Documents/Rails-Projects/Libraries-and-Helpers/Xaaron/.bundle/gems/gems/railties-4.2.0/bin/rails:9:in `<top (required)>'
from bin/rails:16:in `load'
from bin/rails:16:in `<main>'
So I assumed I could do, after doing bundle config --delete bin:
rake rails:update:bin
But then I am given:
rake aborted!
Don't know how to build task 'rails'
I am running this from the root of a rails engine that I have created. I am curious as to whats going on. Should I run this command from the dummy app? I suspect not as the bin/ directory is at the root of the engine directory and not the dummy app.
The correct answer based off the comment to the question is to run:
rake app:rails:update:bin
at the root of the engine.

Rspec command returns '`require': cannot load such file -- spec_helper' when rake spec does not

I am humming along running my unit tests. For each file, I write a test and then run rspec on it to make sure I've written things correctly. I'm using rvm, sourcing ruby v2.1.1. I successfully run the command ruby -S spec/controllers/current_spec.rb
At some point, I run rake spec for all tests
After this, when I run ruby -S spec/controllers/current_spec.rb again I get the following error:
ruby -S spec/controllers/current_spec.rb
/home/eggmatters/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext
/kernel_require.rb:55:in `require': cannot load such file -- spec_helper (LoadError)
from /home/eggmatters/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext
/kernel_require.rb:55:in `require'
from spec/controllers/current_spec.rb:1:in `<main>'
The top of my spec contains:
require 'spec_helper'
Which is throwing the error. spec_helper is where it is supposed to be: spec/spec_helper.rb
Also, when running rake spec, all it does is issue the spec commands for each individual file in the spec directory for example:
$ rake spec:controllers
/home/eggmatters/.rvm/rubies/ruby-2.1.1/bin/ruby -S rspec
./spec/controllers/some_controller_spec.rb
./spec/controllers/another_controller_spec.rb
./spec/controllers/this_controller_spec.rb
./spec/controllers/current_controller_spec.rb
I've verified that ruby -S does indeed point to /home/eggmatters/.rvm/rubies/ruby-2.1.1/bin/ruby -S (which ruby).
The Gemfile looks fine. Running bundle exec ruby -S ./spec/controllers/current_controller_spec.rb throws the same error.
I ran rails generate spec:install already as I started out issuing the command just fine. There were no changes to spec_helper between successful runs.
bundle install doesn't change anything.
What's going on? Why is rspec all of the sudden not finding spec_helper when it is where it needs to be? What did rake do to rspec to all of the sudden break it? Why does rake spec work and this one doesn't?

rack and cucumber; not sure how to resolve

I am having the same issue mentioned here: Rake "already initialized constant WFKV_" warning
I changed my Gemfile to:
gem 'rack', '1.3.3'
If I run:rake cucumber:ok --trace
it looks like it runs:
/Users/jt/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -S bundle exec cucumber --profile default
with the following errors:
rake aborted!
Command failed with status (1): [/Users/jt/.rvm/rubies/ruby-1.9.2-p290/bin/...]
/Users/jt/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/file_utils.rb:53:in `block in create_shell_runner'
/Users/jt/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/file_utils.rb:45:in `call'
/Users/jt/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/file_utils.rb:45:in `sh'
/Users/jt/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/file_utils_ext.rb:36:in `sh'
If I run:
Sat Oct 08$ bundle exec cucumber
things seem to run ok.
How do I fix my gems so that rake cucumber:ok works ok?
thx
edit #1
Is there any way I could confirm which version of Rack that Cucumber is using? Or ensure that Rack 1.3.3 is ALWAYS being used.
You may try creating a temporary gemset and install bundle over there and try it out, following are the step to follow,
rvm gemset create temp_gemset
rvm gemset use temp_gemset
bundle install
Here temp_gemset is the name given to gemset
Have you tried running the rake task via bundle exec?
bundle exec rake cucumber:ok

Resources