Loading namespaced modules in Minitest test files in rails - ruby-on-rails

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

Related

Rails Minitest include to test_helper - uninitialized constant ActiveSupport::TestCase

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

uninitialized constant ApplicationRecord rspec

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.

uninitialized constant ActiveSupport::TestCase

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.

testing rails engine generator with rspec

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'

ConnectionNotEstablished error in custom TestTask

I'm trying to write a custom Rake task to perform some tests for a class placed in the lib directory. This works for basic tests not requiring any models, but I need to actually test using some models. It's my first foray into more advanced rake usage and after going through some other hurdles I've got stuck on getting a ConnectionNotEstablished error.
Here's the rake task:
Rake::TestTask.new(:test => 'db:test:prepare') do |test|
test.libs << 'test/sync'
test.test_files = Dir['test/sync/*_test.rb']
test.verbose = true
end
Here's the test that raise the ConnectionNotEstablished exception:
require 'rubygems'
require 'app/models/city'
require 'foo'
require 'test/unit'
class SyncTest < Test::Unit::TestCase
##sync = Foo::Sync.new('test')
def test_cities
assert City.all.size == 2 # the exception is raised at this point
##sync.cities
assert City.all.size == 102
end
end
Here's a unit test that is actually working:
require 'test_helper'
class CityTest < ActiveSupport::TestCase
test "the truth" do
assert City.all.size == 2
end
end
I've tried using derive my test class from ActiveSupport::TestCase instead of Test::Unit::TestCase but it still raise a ConnectionNotEstablished error. I'm certainly doing something wrong, can anyone find what or tell of a better way to do that?
I've finally found out what the problem was, I've replaced the two first require call by:
require 'test_helper'
and added the test directory to the TestTask's libs attribute:
test.libs << 'test'

Resources