I try to run a simple test following RailsGuides instructions. I have a default structure of the test folder. Here is the relevant part:
test
unit
job_test.rb
test_helper.rb
When I run:
ruby unit/job_test.rb
from the test directory, I get:
<internal:lib/rubygems/custom_require>:29:in `require':
no such file to load -- test_helper (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from unit/job_test.rb:1:in `<main>'
Here is my job_test.rb:
require 'test_helper'
class JobTest < ActiveSupport::TestCase
test "My First Test" do
assert false
end
end
Is this a known Rails 3 issue ?
Any workarounds ?
Try to include test helper like this
require File.dirname(__FILE__) + '/../test_helper'
Related
I am trying to upgrade a Rails 6.1.3.1 application from Ruby 2.6.6 to 3.0.0
All the rspec tests run fine and the in development everything seems to work just fine except one thing:
Even the simplest of tasks fails with this error:
"class variable ##debug_missing_translation of ActionView::Base is overtaken by Object"
For example, this simpletask.rake file
task simpletask: :environment do
puts 'Hello'
end
is not able to run as I get the following error:
lxxx#xxx:~/Workspace/edumino$ rails simpletask
rails aborted!
class variable ##debug_missing_translation of ActionView::Base is overtaken by Object
/home/xxx/railsapp/config/environment.rb:5:in `<top (required)>'
/home/xxx/railsapp/bin/rails:5:in `require'
/home/xxx/railsapp/bin/rails:5:in `<top (required)>'
/home/xxx/railsapp/bin/spring:10:in `require'
/home/xxx/railsapp/bin/spring:10:in `block in <top (required)>'
/home/xxx/railsapp/bin/spring:7:in `<top (required)>'
Tasks: TOP => simpletask => environment
(See full trace by running task with --trace)
...so, it turns out the problem was an already existing task. I don't remember exactly why this was necessary but the task had this:
# frozen_string_literal: true
require "#{Rails.root}/app/helpers/cron_helper"
require "#{Rails.root}/app/helpers/notifier_helper"
require "#{Rails.root}/app/helpers/application_helper"
include ApplicationHelper
include ActionView::Helpers::TranslationHelper
include CronHelper
namespace :cron do
task sms: :environment do |_t, _args|
...
end
end
I assume that the guilty code was including the ActionView::Helpers::TranslationHelper
Again, I can't remember why I had to do that at that time. But if you bump into this kind of 'overtaken by' error when upgrading to Ruby 3.0, check your other tasks and see what kinda weird includes you might have had. In my case, since I didn't need the old task, I just removed it.
I'm trying to do something seemingly simple but it's proven rather hard.
I want to write tests using RSpec for classes that I've put in the lib directory of a Rails Engine.
Here are exactly the steps I'm using:
rails plugin new mygem -T --mountable --full --dummy-path=spec/dummy
Then I cd mygem; vim mygem.gemspec
I add the following line to mygem.gemspec:
s.add_development_dependency "rspec-rails"
I run bundle install; rails generate rspec:install
Then I edit ~/mygem/lib/mygem/engine.rb adding the following:
module Mygem
class Engine < ::Rails::Engine
isolate_namespace Mygem
config.generators do |g|
g.test_framework :rspec
end
end
end
I create a very simple class in the lib directory, ~/mygem/lib/mygem/something.rb
and add the following:
module Mygem
class Something
def hi
"hi"
end
end
end
Create a test file ~/mygem/spec/something_spec.rb
then add the following:
require 'rails_helper'
describe Mygem::Something do
it 'says hi' do
s = Mygem::Something.new
expect(s.hi).to eq('hi')
end
end
And boom, I get the following output:
rspec
~/Documents/mygem/spec/rails_helper.rb:3:in `require': cannot load such file -- ~/Documents/mygem/config/environment (LoadError)
from ~/Documents/mygem/spec/rails_helper.rb:3:in `<top (required)>'
from ~/Documents/mygem/spec/something_spec.rb:1:in `require'
from ~/Documents/mygem/spec/something_spec.rb:1:in `<top (required)>'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load' from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from ~/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `load'
from ~/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `<main>'
from ~/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
from ~/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'
Any tips as to what I may be doing incorrectly?
Thanks in advance!
It looks like your rails_helper is looking for an environment.rb file to load, which doesn't exist in a Rails engine. It does, however, exist in your dummy app, which is what RSpec is run against.
Try adding this into the top of your rails_helper file:
require File.expand_path("../dummy/config/environment.rb", __FILE__)
Finally got it working after the following steps (thanks ccai for the suggestion):
in rails_helper.rb:
# Comment this line:
# require File.expand_path('../../config/environment', __FILE__)
require File.expand_path("../dummy/config/environment.rb", __FILE__)
then in lib/mygem.rb
require "mygem/something"
Works!
When I try to run the following code:
test/models/tweet_test.rb
require 'minitest/spec'
require 'minitest/autorun'
class TweetTest < ActiveSupport::TestCase
test "Tweet should not be null" do
tweet = true
assert tweet
end
end
I receive this error:
tweet_test.rb:4:in `<main>': uninitialized constant ActiveSupport (NameError)
I am following along with a tutorial perfectly. Why is this happening? Has ActiveSuport::TestCase been deprecated?
UPDATE:
I attempted to require 'test_helper' :
require 'minitest/spec'
require 'minitest/autorun'
require 'test_helper'
class TweetTest < ActiveSupport::TestCase
test "Tweet should not be null" do
tweet = true
assert tweet
end
end
But received the following error:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- test_helper (LoadError)
You are not requiring the correct Rails minitest files to setup an ActiveSupport::TestCase. That is a Rails specific class that cannot be found in minitest alone. Are you sure you are working inside a Rails project?
In most cases, you setup a test in Rails by requiring test_helper in your test class so you can inherit from ActiveSupport::TestCase. The file test_helper contains all the requirements and setup for running tests in a Rails project.
Quick fix: just inherit from Minitest::Test.
I have the following script:
#!/usr/bin/ruby
require 'rubygems' unless defined?(Gem)
require 'mongoid'
include Mongoid::Document
#classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a
puts #classes.count
But I always get:
classes_notification.rb:6:in `': uninitialized constant Availability (NameError)
Some help please, I need to make a daemon to send emails with the information on my availability model.
Thanks in advance.
UPDATE CODE
my script is under app/script/user/remeber_classes.rb
how can I access the require File.dirname(FILE) + "/../../config/environment"
because I'm getting
/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in
require': cannot load such file -- ./../../config/environments
(LoadError) from
/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in
require' from remember_class.rb:5:in `'
you must include rails environment first
add require "path_to_the_application/config/environment" to your code
or
you can always access the database directly using mongo gem
see this
https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial
I am having a problem creating a generator in rails this is the error I am getting.
I am trying to create a generator that creates does SMS verification through the provider Twilio.
require "sms_auth-twilio"
NameError: uninitialized constant Twilio::Rails
from /Users/sherodtaylor/.rvm/gems/ruby-1.9.3-p125/gems/sms_auth-twilio-0.0.2/lib/sms_auth-twilio.rb:3:in `<module:Twilio>'
from /Users/sherodtaylor/.rvm/gems/ruby-1.9.3-p125/gems/sms_auth-twilio-0.0.2/lib/sms_auth-twilio.rb:2:in `<top (required)>'
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):1
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'
This is my code
require "sms_auth-twilio/version"
module Twilio
class TwilioGenerator < Rails::Generator::Base
source_root File.expand_path("../generator", __FILE__)
def add_twilio_initializer
copy_file "twilio.rb", "config/initializer/twilio.rb"
end
# config info for the API
def twilio_config_info
copy_file "twilio.yml", "config/twilio.yml"
end
# Verification Controller to verify the company
def copy_verifications
copy_file "verifications_controller", "app/contoller/verifications_controller.rb"
end
end
end
Restarting your Rails server and then try
For your Help see this link : Rails Adding Twilio Sub Accounts : Uninitialized constant User::Twilio
I can't say for sure, but this is not typically how I see generators created within a gem. Usually they are separate from the actual gem_name.rb file. I think if you move the generator logic into the more common lib/generators/generator_name/generator_name.rb you'll have more luck. And then you can place the files to be copied within a subdir ../generator_name/templates/
Also note you need to add the .rb ending to your copy_file "verifications_controller,"
For a template to work off, you can follow the layout of a simple gem I wrote here which utilizes a generator.