Home > Net >  unable to use a compact style of module and class under test folder (NameError)
unable to use a compact style of module and class under test folder (NameError)

Time:11-21

I have folder insider test/fixtures/schemas where I define some schemas that I use to validate the JSON response of some controllers like this:

test
  |
  |
  controllers
  |
  |
  ....
  fixtures
         |
         |
         organizations.yml
         |
         |
         schemas
               |
               |
               clients
                     |
                     |
                     show.rb
               |
               |
               organizations
               ...

the file under fixtures/schemas/client/show.rb looks like this:

module Clients
  class Show < Dry::Validation::Contract
    json do
      required(:id).filled(:string)
      required(:state).filled(:string)
...

it works fine and I can use this schema on my tests with Clients::Show but we also have a rubocop rule that enforces us to use compact module and class style like this:

class Clients::Show < Dry::Validation::Contract

but when I define it like this, I get a NameError

test/fixtures/schemas/client/show.rb:3:in `<top (required)>': uninitialized constant Client (NameError)

I find it weird that one structure works and the other doesn't. I read through the Zeitwerk manual but couldn't anything that would explain my problem, and tried different things like defining a client.rb file with an empty module but it didn't work.

CodePudding user response:

Both versions are not the same.

module Clients
  class Show

defines a module with the name Clients and a class Show in the namespace Clients. But

class Clients::Show

only defines a class Show in the namespace Clients. There isn't automatically a module with the name Clients defined in this case.

  • Related