Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails和RSpec:在不同名称空间(模块)中测试同名控制器_Ruby On Rails_Ruby On Rails 4_Rspec_Ruby 2.2 - Fatal编程技术网

Ruby on rails Rails和RSpec:在不同名称空间(模块)中测试同名控制器

Ruby on rails Rails和RSpec:在不同名称空间(模块)中测试同名控制器,ruby-on-rails,ruby-on-rails-4,rspec,ruby-2.2,Ruby On Rails,Ruby On Rails 4,Rspec,Ruby 2.2,我有一个rails 4.1.16 API应用程序,该应用程序使用RSpec 3.4.0进行测试,在不同模块中测试同名类时遇到问题 结构如下: app/controllers/bar/notifications_controller.rb class Bar::NotificationsController < ApiController ... end 该等级库文件中的所有测试均失败,并出现相同错误: RuntimeError: @controller is nil: mak

我有一个rails 4.1.16 API应用程序,该应用程序使用RSpec 3.4.0进行测试,在不同模块中测试同名类时遇到问题

结构如下:

app/controllers/bar/notifications_controller.rb

class Bar::NotificationsController < ApiController
  ...
end
该等级库文件中的所有测试均失败,并出现相同错误:

RuntimeError:
   @controller is nil: make sure you set it in your test's setup method.
Foo
模块中更改控制器名称时,问题不存在:

app/controllers/foo/bar/foo_notifications_controller.rb

module Foo
  class Bar::FooNotificationsController < ApiController
    ...
  end
end
app/controllers/foo/bar/foo\u notifications\u controller.rb
模块Foo
类栏::FoonNotificationsController
我已经尝试在spec文件
require'bar/notifications\u controller'
的顶部添加,并将类名用作字符串
descriple“bar::notifications controller,type::controller
,但没有解决问题(相同的错误)

为什么会发生这种情况?解决方案是什么

我想相信有一件事我还没有尝试过,我不必为了让规范通过而用毫无意义的名字来污染我的代码和结构


非常感谢您的帮助!

通常,我会在类定义中包含所有名称空间。例如:

app/controllers/foo/bar/notifications_controller.rb

class Foo::Bar::NotificationsController < ApiController
  ...
end
app/controllers/foo/bar/notifications\u controller.rb
类Foo::Bar::NotificationsController
然而,乍一看,这可能与:

app/controllers/foo/bar/notifications_controller.rb

module Foo
  class Bar::NotificationsController < ApiController
    ...
  end
end
app/controllers/foo/bar/notifications\u controller.rb
模块Foo
类栏::NotificationsController
事实上,它们是不同的。不同之处在于Rails如何处理常量的自动加载。我在这里不详细介绍,因为这是一个较长的主题,而且在web-o-sphere中有很多很好的文章/帖子

你可以找到一些关于Rails如何处理自动加载的好文章,比如(或者用谷歌搜索
Rails常量加载


此外,正如文章所指出的,Ruby常量加载的操作与Rails加载不同。可以找到关于Ruby常量加载的好信息(或者尝试谷歌搜索
Ruby常量加载
).

你能尝试使用
模块栏来定义名称空间吗?而不是在类名中定义吗?尝试
描述::栏::通知控制器,类型::控制器做
不幸的是,它不起作用。谢谢,这起作用了!你能简要解释一下为什么会发生这种情况吗?或者该主题的一些资源?我希望模块和以两种方式编写的类的行为方式与它们是相同的:|
app/controllers/foo/bar/notifications_controller.rb

class Foo::Bar::NotificationsController < ApiController
  ...
end
app/controllers/foo/bar/notifications_controller.rb

module Foo
  class Bar::NotificationsController < ApiController
    ...
  end
end