Skip to content

uninitialized constant Anonymou (no s)? #44

@fotanus

Description

@fotanus

Hi,

I'm stuck trying to use an anonymous controller to test my ApplicationController. First I though it was an user error, then posted on StackOverflow, but many programmers that I consider smart took a look and no one could identify as an user error. As a last resort, I'm adding my issue here too because I'm afraid it can be an RSpec bug.

Following here is the description of my problem on SO:


I'm testing my application controller with RSpec using an anonymous controller (which I'm not sure is the standard practice, but this is what Google led me to).

This is the full RSpec with only one test, that is failing:

    require "spec_helper"


    shared_examples_for "normal errors for html format" do |action, expected_error|
      it "raises the error" do
        expect { get action}.to raise_error(expected_error)
      end
    end

    describe ApplicationController do
      controller do
        def runtime_error
          raise RuntimeError
        end

        def handable_json_error
          raise ApplicationController::InvalidParams, "model"
        end
      end

      before do
        @routes.draw do
          get 'anonymous/runtime_error'
          get 'anonymous/handable_json_error'
        end
      end

      describe "#handle_exceptions" do
        context "when runtime error happens" do
          describe "with http format" do
            it_behaves_like "normal errors for html format", :runtime_error, RuntimeError
          end
        end
      end
    end

And my spec helper:

    # This file is copied to spec/ when you run 'rails generate rspec:install'
    require 'simplecov'
    SimpleCov.start 'rails'

    ENV["RAILS_ENV"] ||= 'test'

    require File.expand_path("../../config/environment", __FILE__)
    require 'rspec/rails'
    require 'rspec/autorun'
    require 'authlogic/test_case'

    include Authlogic::TestCase

    Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

    RSpec.configure do |config|
      config.include FactoryGirl::Syntax::Methods

      config.fixture_path = "#{::Rails.root}/spec/fixtures"
      config.use_transactional_fixtures = true

      config.infer_base_class_for_anonymous_controllers = true

      config.order = "random"
    end

I noticed that the backtrace didn't even hit my controller, so I don't know if is related.
The output of RSpec with -b, to get the full trace:

rspec -b spec/controllers/application_controller_spec.rb 
F

Failures:

  1) ApplicationController#handle_exceptions when runtime error happens with http format behaves like normal errors for html format raises the error
     Failure/Error: expect { get action}.to raise_error(expected_error)
       expected RuntimeError, got #<NameError: uninitialized constant Anonymou> with backtrace:
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:147:in `resource_class'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:154:in `resource_class_with_parent'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:41:in `authorize_resource'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:429:in `_run__1066669752__process_action__49729610__callbacks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/abstract_controller/callbacks.rb:17:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `block in instrument'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `instrument'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/abstract_controller/base.rb:121:in `process'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:in `process'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:475:in `process'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:49:in `process'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:392:in `get'
         # ./spec/controllers/application_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/raise_error.rb:22:in `call'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/raise_error.rb:22:in `matches?'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/handler.rb:23:in `handle_matcher'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/expectation_target.rb:34:in `to'
         # ./spec/controllers/application_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `instance_eval'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:87:in `block (2 levels) in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `call'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:329:in `run_around_each_hooks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:111:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:390:in `block in run_examples'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `run_examples'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:371:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `map'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block in run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
         # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
     Shared Example Group: "normal errors for html format" called from ./spec/controllers/application_controller_spec.rb:31
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/fail_with.rb:32:in `fail_with'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/handler.rb:33:in `handle_matcher'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-expectations-2.13.0/lib/rspec/expectations/expectation_target.rb:34:in `to'
     # ./spec/controllers/application_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `instance_eval'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:87:in `block (2 levels) in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `call'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:329:in `run_around_each_hooks'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:111:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:390:in `block in run_examples'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `run_examples'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:371:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `map'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block in run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
     # /home/fotanus/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'

Finished in 0.052 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/application_controller_spec.rb:5 # ApplicationController#handle_exceptions when runtime error happens with http format behaves like normal errors for html format raises the error

Randomized with seed 2493

Coverage report generated for RSpec to /home/fotanus/work/translation_proxy/coverage. 236 / 548 LOC (43.07%) covered.

I'm using Rails 3.2.13 and rspec 2.13.1

I haven't write Anonymou anywhere. See the following output:

grep -rin --exclude-dir=log --binary-files=text Anonymou .
./tmp/cache/assets/DE8/730/sprockets%2Fa51ac8eb9ee0a56c6c04ae1c21184baf:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/DE8/730/sprockets%2Fa51ac8eb9ee0a56c6c04ae1c21184baf:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./tmp/cache/assets/C8A/4B0/sprockets%2Ff16dfcb3e634f5416377592c25469801:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/C8A/4B0/sprockets%2Ff16dfcb3e634f5416377592c25469801:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./tmp/cache/assets/D78/320/sprockets%2F95003f96fcb4963ddf27265db635eabe:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/D78/320/sprockets%2F95003f96fcb4963ddf27265db635eabe:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./tmp/cache/assets/CD9/940/sprockets%2F1b1af54f9943c03a292c5d52227619dd:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/CD9/940/sprockets%2F1b1af54f9943c03a292c5d52227619dd:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994:612:            // We use an anonymous function so that context is window
./tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994:9800:   // understands anonymous AMD modules. A named AMD is safest and most robust
./spec/spec_helper.rb:22:  config.infer_base_class_for_anonymous_controllers = true
./spec/controllers/application_controller_spec.rb:23:      get 'anonymous/runtime_error'
./spec/controllers/application_controller_spec.rb:24:      get 'anonymous/handable_json_error'

What is wrong?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions