Home > front end >  Use anonymous class generated by struct in ruby tests
Use anonymous class generated by struct in ruby tests

Time:09-27

So, here we have a file with struct,

module CoreDomain
  Corporation = Struct.new(...)
end

and we have such a test file,

require 'test_helper'

module CoreDomain
  class CorporationTest < ActiveSupport::TestCase
    def test_corporation_struct_creation
      corp_struct = CoreDomain::Corporation.new(...)

      assert_equal ..., ...
    end
  end
end

when I trying to execute the test I get this error.

NameError: uninitialized constant CoreDomain::Corporation

Question - where I am getting wrong?

CodePudding user response:

For this to be found you would have to change your module to this:

module CoreDomain
  class Corporation
    Corp = Struct.new(...)
  end
end

Update

The double semicolon is incorrect because it refers only to subclasses of the module. You are creating an attribute, not a class.

module CoreDomain
  class << self
    attr_accessor :corporation
  end
  self.corporation = Struct.new(...)
end

You shouldn't capitalize attribute names in Ruby either. Capitalization is for class and module names, the former I thought you were trying to achieve.

  • Related