0

I'm writing a class inside a module

#lib/app/models/foo.rb
module App::Models
  class Foo
    def bar
      true
    end
  end
end

and when i tried to run the spec for it

#spec/lib/app/models/foo_spec.rb
require_relative '../../../../lib/app/models/foo'

describe App::Models::Foo do
end

i get the follow error:

rspec spec/lib/app/models/foo_spec.rb
/Users/frojas/git/tmp/lib/app/models/foo.rb:1:in `<top (required)>': uninitialized constant App (NameError)
    from /Users/frojas/git/tmp/spec/lib/app/models/foo_spec.rb:1:in `require_relative'

I don't quite understand want i'm doing wrong.

1
  • Where did you define App ? You first need to define it.. Then module App::Models this should come.. Commented Jan 28, 2014 at 14:16

2 Answers 2

1

You have to define a module before using it:

module App
  module Models
    class Foo
      def bar
        true
      end
    end
  end
end

This way you are defining the App module, then the Models module and then the Foo class properly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! it's not the namespacing i'm using, it was just the first example I came up with.
1

module App::Models means you are defining Models module inside App module. But Before doing this you need to define the module App also. As you didn't do that, so the error uninitialized constant App (NameError) it bubbles up.

First

module App
  # your code
end

Then

module App::Models
  class Foo
    def bar
      true
    end
  end
end

Or do as below :

module App
  module Models
    class Foo
      def bar
        true
      end
    end
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.