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.
Related
I want to add helper from test/helpers/auth_request_helpers.rb into test/test_helper.rb to have it available in all tests. I thought all I had to do was to include this helper inside of the test_helper like below:
module ActiveSupport
class TestCase
include AuthRequestHelpers
# some other things
end
end
But when I running a minitest I'm getting an error:
uninitialized constant ActiveSupport::TestCase::AuthRequestHelpers (NameError)
Did I missed something? I'm not using RSpec
Helper which I tried to add below:
module AuthRequestHelpers
def hmac_code(data, secret_key)
Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', secret_key, data))
end
end
Rails 5.2.1 with minitest 5.1
I have a file app/strategies/foo/bar/baz.rb that I want to namespace
baz.rb:
module Foo
module Bar
class Baz
in rails console I can successfully run:
test = Foo::Bar::Baz.new
in testing /test/strategies/foor/bar/baz_test.rb:
require 'test_helper'
class BazTest < ActiveSupport::TestCase
test 'I can call it' do
test = Foo::Bar::Baz.new
end
and I get:
NameError: uninitialized constant BazTest::Foo
I've tried varies forms of require at the top of baz_test.rb
require '/app/strategies/foo/bar/baz'
require '../../../../app/strategies/foo/bar/baz'
require 'baz'
All with the same error *** LoadError Exception: cannot load such file
Any suggestions would be appreciated!
Figured it out!
/test/strategies/foo/bar/baz_test.rb class definition needed to be:
class Foo::Bar::BazTest < ActiveSupport::TestCase
1) created a model called Skill
2) ran some seeds
3) ran rspec --init
4) created file skill_spec.rb with the code below
require_relative "../app/models/skill"
describe Skill do
describe "database" do
it "should have 42 skills" do
expect(Skill.all.count).to eq(42)
end
end
end
5) when I run rspec in console get error:
Failure/Error: class Skill < ApplicationRecordNameError:
uninitialized constant ApplicationRecord
I already have a file application_record.rb with the following code
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
For rails specs use require 'rails-helper' at beginning of each spec file (it is generated by bin/rails generate rspec:install from rspec-rails gem)
It contains line require File.expand_path('../config/environment', __dir__) that will load your rails environment and you'll have autoloading and all other rails parts working.
I've got some work to do refactoring a mailer class. I don't want to spend all my time loading rails and waiting to see if tests are passing. I'd like to just not include spec_helper and speed up my tests.
How do I just include ActionMailer?
I tried this:
require 'action_mailer'
but I stil end up with this error:
uninitialized constant ActionMailer (NameError)
Any ideas?
I'm using MiniTest, and the following works fine for me. You may be able to extrapolate from this, or provide some more info in your question.
require 'minitest/autorun'
require 'mocha'
require "minitest-matchers"
require 'action_mailer'
require "email_spec"
require File.expand_path('../../../../app/mailers/my_mailer', __FILE__)
class MyMailerTest < MiniTest::Unit::TestCase
include EmailSpec::Helpers
include EmailSpec::Matchers
def test_it_is_sent_from_me
email = MyMailer.refund_processed(42)
email.must be_delivered_from("me#example.com")
end
end
I create a simpe gem wich include a install generator, generator works fine but now I want to test it using rspec, I foud this gem, and try to test my generator, my spec code is:
require 'genspec'
require 'rosalie'
describe :install_generator do
it "should generate model" do
subject.should generate("message.rb")
end
end
rosalie is the name of may gem, now when I run it I got an error:
/stuff/work/my_projects/rosalie/lib/rosalie/engine.rb:2:in `': uninitialized constant Rosalie::Rails (NameError)
my engine.rb code is:
module Rosalie
class Engine < Rails::Engine
initializer "rosalie.models.messageable" do
ActiveSupport.on_load(:active_record) do
include Rosalie::Models::Messageable
end
end
end
end
anybody can help me with this problem?
You have to load your code before you include it somewhere.
Either require or autoload your main file.
Here is an example from my gem.
You need add these code in your spec_helper.rb, and require the spec_helper in each spec.
require File.expand_path("../dummy/config/environment", __FILE__)
require 'rspec/rails'