Rspec 测试在命名空间中失败
我刚刚将一些现有的 Rails 测试转换为 rspec,现在命名空间中的测试失败了。
即在下面的示例中,AccountController 规范通过,而 ChildrenController 失败并出现以下错误:
in `load_missing_constant': Expected /.../app/controllers/admin/children_controller.rb to define Admin::ChildrenController (LoadError)
app/controllers/account_controller.rb
class AccountController < ApplicationController
spec/controllers/account_controller_spec.rb
require 'spec_helper'
describe AccountController do
#...
end
app/controllers/admin/children_controller.rb
class Admin::ChildrenController < ApplicationController
spec/controllers/admin/children_controller_spec。 rb
require 'spec_helper'
describe Admin::ChildrenController do
include ::ControllerHelper
#...
end
我正在使用
- ruby-1.9.2-p0
- Rails 3.0.3
- rspec 2.3.0
我尝试过使用命名空间定义,但到目前为止没有运气 - 有什么想法吗???
I've just converted some existing rails tests to rspec, and now the tests that are in a namespace fail.
I.e. in the example below, AccountController spec passes, while the ChildrenController fails with the following error:
in `load_missing_constant': Expected /.../app/controllers/admin/children_controller.rb to define Admin::ChildrenController (LoadError)
app/controllers/account_controller.rb
class AccountController < ApplicationController
spec/controllers/account_controller_spec.rb
require 'spec_helper'
describe AccountController do
#...
end
app/controllers/admin/children_controller.rb
class Admin::ChildrenController < ApplicationController
spec/controllers/admin/children_controller_spec.rb
require 'spec_helper'
describe Admin::ChildrenController do
include ::ControllerHelper
#...
end
I'm using
- ruby-1.9.2-p0
- Rails 3.0.3
- rspec 2.3.0
I've tried playing with the namespace definitions, but no luck so far - any ideas???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另一个解决方案:
通过将类定义为字符串,它将正常加载:
这将在spec/controller/admin目录中工作
编辑:在2.10.x中不起作用
Another solution:
by defining the class as a string it will load normally:
this will work in the spec/controller/admin directory
edit: doesnt work in 2.10.x
我遇到了同样的问题,并且不愿意将测试放在较低的目录中。就我而言,是 Spork 搞砸了事情。
准确地说:
如果 spork 正在运行,我放置了一个检查器,否则你应该忽略这一行。
I had the same problem, and was not willing to place the tests in a lower directory. In my case it was Spork that was messing things up.
To be precise:
I placed a checker if spork is running, else you should ignore this line.
发布答案以防有人再次遇到这个问题!
最后我通过压平规格来修复它,如下所示:
Posting answer in case anyone stumbles over this another time!
In the end I fixed it by flattening the specs like following:
我遇到了同样的问题并通过以下方式解决了它:
之前:
运行 rspec 给了我:
之后:
I had the same problem and solved it in the following way:
Before:
Running rspec gave me:
After:
您可以将控制器保存在单独的文件夹下,但您必须使用
需要 File.dirname(FILE) + '/../../spec_helper'
而不是仅仅需要“spec_helper”
You can keep controller under separate folder, but you have to use
require File.dirname(FILE) + '/../../spec_helper'
instead of just require 'spec_helper'