Rails 7 and Zeitwerk not loading in tests

Viewed 243

My Rails 7, Ruby 3.1 App has a simple test that is failing. I am not understanding why.

In my App I have added a support directory labelled stuff, which has some GraphQL code in it. So in my tests, I want to do a GraphQL.test(foo: "me", bar: "you").

app/stuff/graphql.rb

In my inflections.rb initializer, I teach the Inflection I want.

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'GraphQL'
end

In my application.rb, I tell Zeitwerk that I have stuff to work with, and the rake task zeitwerk:check says everything is good.

The graphql.rb has a simple test function:

module GraphQL
  def self.test(foo:, bar:)
    puts "We got a #{foo} and a #{bar}"
  end
end

But my tests run, and when testing out this code, the test fails on GraphQL not having a test method.

I am confused by the autoloading and eager loading, and why my tests seem unable to follow the simple setup here.

1 Answers

It seems this was due to namespace issues with Zeitwerk. In my tests, I had a require that introduced the same namespace on a different object, namely GraphQL. So when in the tests, the named method was not found, as the Object returned was in the wrong namespacce. I removed the require, and the tests completed as expected, with no errors.

Related