Rspec Controllers in and out of namespace with same name

后端 未结 1 1541
执念已碎
执念已碎 2020-12-06 02:48

I have the following setup:

class UsersController < ApplicationController
...
end

class Admin::BaseController < ApplicationController
...
end

class A         


        
1条回答
  •  感情败类
    2020-12-06 03:07

    This happens when a top level class get autoloaded before a namespaced one is used. If you have this code without any class preloaded :

    UsersController
    module AdminArea
      UsersController
    end
    

    The first line will trigger constant missing hook : "ok, UsersController does not exist, so let's try to load it".

    But then, reaching the second line, UsersController is indeed already defined, at top level. So, there's no const_missing hook triggered, and app will try to use the known constant.

    To avoid that, explicitly require proper classes on top of your spec files :

    #spec/controllers/users_controller_spec.rb:
    
    require 'users_controller'
    

    And

    #spec/controllers/admin/users_controller_spec.rb
    
    require 'admin/users_controller'
    

    0 讨论(0)
提交回复
热议问题