Rspec 测试在命名空间中失败

发布于 2024-10-09 02:48:34 字数 1021 浏览 1 评论 0原文

我刚刚将一些现有的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感性 2024-10-16 02:48:34

另一个解决方案:

通过将类定义为字符串,它将正常加载:

# children_controller_spec.rb
require 'spec_helper'
describe "Admin::ChildrenController" do  
  # -something-
end

这将在spec/controller/admin目录中工作

编辑:在2.10.x中不起作用

Another solution:

by defining the class as a string it will load normally:

# children_controller_spec.rb
require 'spec_helper'
describe "Admin::ChildrenController" do  
  # -something-
end

this will work in the spec/controller/admin directory

edit: doesnt work in 2.10.x

不知在何时 2024-10-16 02:48:34

我遇到了同样的问题,并且不愿意将测试放在较低的目录中。就我而言,是 Spork 搞砸了事情。

准确地说:

Spork.each_run do
  ActiveSupport::Dependencies.clear
end

如果 spork 正在运行,我放置了一个检查器,否则你应该忽略这一行。

Spork.each_run do
  if /spork/i =~ $0 || RSpec.configuration.drb?
    ActiveSupport::Dependencies.clear
  end
end

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:

Spork.each_run do
  ActiveSupport::Dependencies.clear
end

I placed a checker if spork is running, else you should ignore this line.

Spork.each_run do
  if /spork/i =~ $0 || RSpec.configuration.drb?
    ActiveSupport::Dependencies.clear
  end
end
虚拟世界 2024-10-16 02:48:34

发布答案以防有人再次遇到这个问题!

最后我通过压平规格来修复它,如下所示:

app>controllers>admin>children_controller.rb
class Admin::ChildrenController < ApplicationController

spec>controllers>children_controller_spec.rb
require 'spec_helper'
describe Admin::ChildrenController do  

Posting answer in case anyone stumbles over this another time!

In the end I fixed it by flattening the specs like following:

app>controllers>admin>children_controller.rb
class Admin::ChildrenController < ApplicationController

spec>controllers>children_controller_spec.rb
require 'spec_helper'
describe Admin::ChildrenController do  
我乃一代侩神 2024-10-16 02:48:34

我遇到了同样的问题并通过以下方式解决了它:

之前:

# app/controllers/admin/awards_controller.rb:
class Admin::AwardsController < ApplicationController

# spec/controllers/admin/awards_controller_spec.rb:
require 'spec_helper'

describe Admin::AwardsController do

运行 rspec 给了我:

/Users/andy/.rvm/gems/ruby-1.9.3-p385@xxxx/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:503:in `load_missing_constant': Expected /Volumes/untitled/xxxx/app/controllers/admin/awards_controller.rb to define Admin::AwardsController (LoadError)
(stacktrace...)

之后:

# spec/controllers/admin/awards_controller_spec.rb:
require 'spec_helper'
load "#{Rails.root}/app/controllers/admin/awards_controller.rb"

describe Admin::AwardsController do

I had the same problem and solved it in the following way:

Before:

# app/controllers/admin/awards_controller.rb:
class Admin::AwardsController < ApplicationController

# spec/controllers/admin/awards_controller_spec.rb:
require 'spec_helper'

describe Admin::AwardsController do

Running rspec gave me:

/Users/andy/.rvm/gems/ruby-1.9.3-p385@xxxx/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:503:in `load_missing_constant': Expected /Volumes/untitled/xxxx/app/controllers/admin/awards_controller.rb to define Admin::AwardsController (LoadError)
(stacktrace...)

After:

# spec/controllers/admin/awards_controller_spec.rb:
require 'spec_helper'
load "#{Rails.root}/app/controllers/admin/awards_controller.rb"

describe Admin::AwardsController do
百思不得你姐 2024-10-16 02:48:34

您可以将控制器保存在单独的文件夹下,但您必须使用
需要 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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文