Rails Minitest include to test_helper - uninitialized constant ActiveSupport::TestCase - ruby-on-rails

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

Related

Updating a gem having issues with zeitwerk

I'm trying to update this gem to work with rails 7 and I think that the error is due to zeitwerk. The error that's popping up is the following
/Users/nate/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/impressionist-2.0.0/lib/impressionist/engine.rb:15:in `block (2 levels) in <class:Engine>': uninitialized constant Impressionist::Engine::ImpressionistController (NameError)
and It's originating from lib/impressionist/engine.rb here from the lines below
module Impressionist
class Engine < ::Rails::Engine
attr_accessor :orm
initializer 'impressionist.model' do |app|
#orm = Impressionist.orm
include_orm
end
initializer 'impressionist.controller' do
require "impressionist/controllers/mongoid/impressionist_controller" if orm == :mongoid.to_s
ActiveSupport.on_load(:action_controller) do
include ImpressionistController::InstanceMethods <--- HERE
extend ImpressionistController::ClassMethods <--- HERE
end
end
private
def include_orm
require "#{root}/app/models/impressionist/impressionable.rb"
require "impressionist/models/#{orm}/impression.rb"
require "impressionist/models/#{orm}/impressionist/impressionable.rb"
end
end
end
I think that this first include is trying to call app/controllers/impressionist_controller.rb, but for some reason doesn't load with rails 7. Any ideas on how to approach fixing this? I suspect that I have to somehow incorporate zeitwerk here.

Loading namespaced modules in Minitest test files in 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

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'

Rails Resque undefined method error in external module

I'm having trouble calling methods from an included module inside a resque worker. In the example below, I keep getting undefined method errrors when I attempt to call the say method inside the worker (which is in the TestLib module). I've reduced the code down to bare basics to illustrate the issue:
Controller
(/app/controllers/test_controller.rb)
class TestController < ApplicationController
def testque
Resque.enqueue( TestWorker, "HI" )
end
end
Library
(/lib/test_lib.rb)
module TestLib
def say( word )
puts word
end
end
Worker
(/workers/test_worker.rb)
require 'test_lib'
class TestWorker
include TestLib
#queue = :test_queue
def self.perform( word )
say( word ) #returns: undefined method 'say' for TestWorker:Class
TestLib::say( word ) #returns: undefined method 'say' for TestLib::Module
end
end
Rakefile
(resque.rake)
require "resque/tasks"
task "resque:setup" => :environment
I'm running resque using the following command: rake environment resque:work QUEUE='*'
Gems:
rails (3.0.4)
redis (2.2.2)
redis-namespace (1.0.3)
resque (1.19.0)
Server:
nginx/1.0.6
Anyone have any ideas as to what's going on there?
When you include a module, its methods become instance methods. When you extend, they become class methods. You just need to change include TestLib to extend TestLib and it should work.

Resources