1

I have a rails App. I have this file app/errors/status.rb

I try to pass the test but is not not working.

module Errors 
  class Status
    def initialize status
      @status = status
    end

    def default_message
      "Error in the server status: #{status}"
    end

    private

    attr_reader :status
  end
end

And the test on spec/errors/status_spec.rb:

require 'rails_helper'

describe Errors::Status do
  let(:status) { double 'status' }

  subject { described_class.new status }

  describe 'default_message' do
    it 'returns the default message' do
      expect(subject.call).to eq( "Error in the server status: #{status}")
    end
  end
end

And it keeps throwing this error:

/Users/gerardmorera/bet_play/spec/errors/status_spec.rb:3:in `<top (required)>': uninitialized constant Errors (NameError)
    from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'
    from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
    from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
    from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
    from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
    from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
1
  • Your Rails version is ? Commented Oct 31, 2015 at 14:31

1 Answer 1

1

That’s because of how ActiveSupport’s auto-loading works and the way in which Rails sets up the $LOAD_PATH. Autoload sees Errors::Status and expects to find it at errors/status somewhere via require, but it doesn’t because app/errors is in the $LOAD_PATH, so you would require your file with just require 'errors'.

You can fix this by moving app/errors/status.rb to a location ActiveSupport’s auto-loading expects (e.g. app/<something>/errors/status.rb). You can puts $LOAD_ATH to see all the possible locations (note that Rails will add all directories in app/ to the $LOAD_PATH).

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

5 Comments

This answer is good, and more detail discussion .
here is a sweet reasoning
@Andrew is there any way to fix it without modifying the tree structure? I am not sure if I still understand the problem. I have other folders at the same level, like services, components, null_object and they do not give any problem...
Nope. Not without making ActiveSupport::Dependencies very angry. Think about it: app/models/foo.rb does not contain Models::Foo, and likewise it expects app/errors/status.rb to contain Status, not Errors::Status.
To expand Andrew's comment, considering that app/models/foo.rb contains FooModel, and and app/controllers/foo.rb contains FooController, you can rename the error class from Errors::Status to StatusError. This maintains consistency with both the Rails class naming scheme and Ruby's error naming scheme.

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.