Rails tests can't find test_helper - ruby-on-rails

I'm trying to run individual tests through ruby test/unit/mytest.rb, but I always get a "no such file to load - test_helper" error. Google brought up a few suggestions, but none of them worked for me. I'm running Rails 3.0, Ruby 1.9.2 (through RVM) on Ubuntu 10.10
Here's what I've tried so far - any suggestions really appreciated
Changed the "require test_helper" to "require File.dirname(FILE) + "/../test_helper"
" in test/unit/mytest_test.rb. It brings back " no such file to load -- test/unit/../test_helper"
Tried running rvm test/unit/mytest_test.rb Same as above
Tried running ruby -I test/unit/mytest_test.rb. No messages to the terminal. After about 5 minutes waiting for something to happen, ctrl+c'd out of it
Any suggestions very appreciated - I'm stumped.

ruby 1.9.2 removed ".", the current directory, from the load path. I have to do this to get it to work:
require 'test_helper'
and call it like:
ruby -I. unit/person_test.rb

I was fighting this thing myself today and i dislike the big require with whole path to file and stuff...
In my case it was fault of Rakefile..
so now it looks like this:
require "bundler/gem_tasks"
require "rake/testtask"
Rake::TestTask.new do |t|
t.libs << "lib"
t.libs << "test" # here is the test_helper
t.pattern = "test/**/*_test.rb"
end
task default: :test
I know its old and has answer marked accepted, but maybe this will also help someone :)
have a nice day

I've added the following to the top of my test files.
require File.expand_path("../../test_helper", __FILE__)
This restores the previous behavior and allows the call to be simply:
ruby test/unit/person_test.rb

Maybe you should run your test cases in this way:
$ rake test
There is no need to change the "require" statement from generated code if you use rake.
Tested with Ruby 1.9.3 and Rails 3.2.8

If you are creating a gem or engine, running rake test in the test dummy application directory will cause this error. Running rake test in the root of the gem will avoid this.

Rails 1.9 no longer includes the current directory in the LOAD_PATH, which causes this problem. You have a few options.
call the test with the -I option from the app dir:
ruby -I test test/functional/test_foo.rb
and use a require with no path:
require "test_helper.rb"
use a full path in the require.  Either
require 'pathname'
require Pathname.new(FILE).realpath.dirname.join('/../test_helper.rb')
or
require (File.dirname(File.realdirpath(__FILE__)) + '/../test_helper.rb')

Related

What can be done to fix the following error when we run any rails command: " `require_relative': cannot load such file "

Any rails command doesn't work for me. I have several versions of ruby installed through rvm. I tried installing rails with all the versions, they do install successfully but with all of them I face the following error whenever I run any rails command in my project directory:
~ rails new blog
Traceback (most recent call last):
1: from bin/rails:3:in `<main>'
bin/rails:3:in `require_relative': cannot load such file -- /Users/Am33d/Documents/config/boot (LoadError)
I tried looking up for the error but didn't find any solutions.
What can be done to fix this? I am using macOS Mojave (10.14.6)
This error would indicate that you do not have a boot.rb file in your config directory for some reason. When running rails commands -- regardless of if you run them as bin/rails [command] or bundle exec rails [command], runs the bin/rails file. This file typically has a line require_relative '../config/boot. The boilerplate bin/rails file in a new Rails 6 app is:
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
To simply fix this you can create a blank file by running touch config/boot.rb from the root directory of your application and that would itself suppress the error. Having said that, you'd be better off creating a config/boot.rb file that contains useful information. The boilerplate config/boot.rb file is this
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
Without this file you are not necessarily appropriately loading the gems from your gemfile or caching operations with bootsnap.
When I ran into this problem, I also received the same error when trying to use rails -s.
If the same is happening for you, its because your version of ruby isn't compatible with your rails version.
After upgrading to the latest rails version the error stopped and everything worked well.
A little bit weird.
rails new blog should not need to find boot file, neither need bundler. Actually, you are trying to create a new project, so it is expected there is not Gemfile, boot, or bundler created for your project.
So I would advise you to find what rails command is being executed. Sometimes one rails executable created by bundler is taking precedence in the $PATH environment variable.
Ex: some incorrect bin/rails executable is on your $HOME directory.
i was trying to build an app using 'rails server' and this error was showing. You have to make sure that the rails version matches the ruby version and even if you do what an answer above said (create the config/boot.rb paste) doesnt work, than you have to change the version of rails to the stable. I'll let the link of this problem here, there's an issue closed in github for this problem https://github.com/rails/rails/pull/43951
to solve the problem you have to replace the gem rails line on the gemfile to this:
gem "rails", github: "rails/rails", branch: "7-0-stable"
Sorry about my english.

No ANSI color for rails tests, running windows 10

I cannot seem to get the rails tests to display in color. I am using rails version 5.0.0.rc2, running windows 10. I have installed the minitest gem, but I am getting this message:
ansi: 'gem install win32console' to use color on Windows
I did try to install win32console, this had no effect. I also noticed that win32console is deprecated, so I installed ansicon as recommended, but this did not help either. I know ansicon is working because I see colors for other actions, such as running "bundle install", yet I am still getting black and white in my tests, along with the error to install win32console. Am I missing something here? Or is this an incompatibility with the testing framework? Any help or insight would be greatly appreciated. Here is my test_helper.rb file if that helps.
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/reporters'
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
I'm using Cmder, not plain Window's cmd.exe. However, this might work if you have the ansi gem installed or it might at least give you an idea:
Locate the file ...Ruby22\lib\gems\2.2.0\gems\ansi-1.5.0\lib\ansi\code.rb, notice near the top there is a check for the Windows platform, edit this to look something like this:
if RUBY_PLATFORM =~ /(win32|w32)/
if (ENV['ANSICON'])
$ansi = true
else
begin
require 'Win32/Console/ANSI'
rescue LoadError
warn "ansi: 'gem install win32console' to use color on Windows"
$ansi = false
end
end
You may need to change the 'ANSICON' to something similar that is in your environment.
I'm new to rails and just figuring out things so this may be a newbie reason for getting this error, but I had to put the win32console gem in my gemfiles and run bundle install. Installing gems doesn't work with rails, at least for me. Once I did that colors work fine. I am running platform 'x64_mingw' which might have something to do with it.

Rake spec fails - Name error, Load error

new to rspec, need to work on a test, when I run rake spec I get these errors, searched thru a bunch of old similar issues but nothing is working. not sure what to do...please help.
ruby -v 2.1.2
rails -v 4.1.4
http://pastebin.com/VJUh0Hhg
Thanks.
I guess the file located at /Users/jessep/work/noterizer/spec/helpers/notes_helper_spec.rb have an issue in the line 2
probably it looks like:
require File.expand_path("../../config/environment", __FILE__)
to fix the issue just add ../ so should look like:
require File.expand_path("../../../config/environment", __FILE__)

Rspec: Could not find minitest-4.7.5 in any of the sources

This is bizarre because I am relatively certain that minitest is an independent testing framework from rspec.
In any case, what is happening is that when I run.
rspec at my rails root directory, I get this error:
Could not find minitest-4.7.5 in any of the sources
However in my Gemfile I have the following:
gem 'minitest', '~> 4.7.5'
I have tried running bundle update and bundle install to no avail. Also I feel weird even adding the gem file as I only added it after I got the error but had no intention of using the gem to begin with.
For reference here is my spec_helper.rb file.
require 'rubygems'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
Hard to know w/o more information, it's likely something else is bringing it in if you aren't... Do this:
Revert to a clean state in your repository
Remove minutest from Gemfile
Run bundle (not update)
Look in your Gemfile.lock and see what is depending upon minitest
Post the information here if the answer doesn't become clear.
You may also want re-run rails generate rspec:install, that spec_helper.rb you posted looks pretty thin.
It turns out I just needed to run bundle exec rspec to run my tests.

Calling a ruby script from Rails with different gemfile

I have a ruby script I wrote which generates data and loads it to MongoDB. I am now trying to call this load script from seed.rb of my Rails app (so I can run via rake db:seed)
I attempted to call it from rails using this code:
system( "ruby data_load/db_load.rb -a data_load/doc1.json" )
When that code executes, I get the following error. (Note it runs fine from the command line):
data_load/db_load.rb:15:in `require': cannot load such file -- mongo (LoadError)
from data_load/db_load.rb:15:in `<main>'
The top of db_load.rb looks like this:
# includes gems from gemfile
require 'rubygems'
require 'bundler/setup'
Bundler.setup
require 'mongo'
require_relative 'load_scripts/cmd_options'
require_relative 'load_scripts/build_index'
....
include Mongo
The script has it's own gemfile in the data_load directory.
My guess is ruby is running the script using the bundle for the rails application instead of the shell script.
Any suggestions on how I can execute this script?
I believe the problem is where Bundler is looking for the Gemfile. Since your script is being run in the parent directly it is finding the Gemfile for the main app.
Set the BUNDLE_GEMFILE before calling your script:
system "BUNDLE_GEMFILE=data_load/Gemfile ruby data_load/db_load.rb -a data_load/doc1.json"
I'm sorry but I think you can't do it, unless you run the script as a different process (like a shell command), doing it is easy:
`shell_command params`
Just use the correct path and params.
Otherwise, consider that a gemfile is "more or less" at its basic level, a bunch of require (or load) statements, so loading a different gemfile would overwrite the original one, creating a lot of issue with rails after that.
The subprocess command is a good idea, however you can only pass string as params, not in-memory objects.

Resources