简体   繁体   English

Rspec测试在命名空间中失败

[英]Rspec tests fail in Namespace

I've just converted some existing rails tests to rspec, and now the tests that are in a namespace fail. 我刚刚将一些现有的rails测试转换为rspec,现在命名空间中的测试失败了。

Ie in the example below, AccountController spec passes, while the ChildrenController fails with the following error: 也就是说,在下面的示例中,AccountController规范通过了,而ChildrenController失败并出现以下错误:

in `load_missing_constant': Expected /.../app/controllers/admin/children_controller.rb to define Admin::ChildrenController (LoadError)

app/controllers/account_controller.rb app / controllers / account_controller.rb

class AccountController < ApplicationController

spec/controllers/account_controller_spec.rb spec / controllers / account_controller_spec.rb

require 'spec_helper'

describe AccountController do
  #...
end

app/controllers/admin/children_controller.rb app / controllers / admin / children_controller.rb

class Admin::ChildrenController < ApplicationController

spec/controllers/admin/children_controller_spec.rb 规范/控制器/管理员/children_controller_spec.rb

require 'spec_helper'

describe Admin::ChildrenController do  
   include ::ControllerHelper 
   #... 
end

I'm using 我正在使用

  • ruby-1.9.2-p0 红宝石1.9.2-p0
  • Rails 3.0.3 Rails 3.0.3
  • rspec 2.3.0 规范2.3.0

I've tried playing with the namespace definitions, but no luck so far - any ideas??? 我试过使用名称空间定义,但到目前为止还没有运气-有任何想法吗???

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 这将在spec / controller / admin目录中工作

edit: doesnt work in 2.10.x 编辑:不适用于2.10.x

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. 就我而言,正是Spork搞砸了。

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正在运行,我放置了一个检查器,否则您应该忽略此行。

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

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: 运行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...)

After: 后:

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

describe Admin::AwardsController 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  

您可以将控制器放在单独的文件夹下,但必须使用require File.dirname( FILE )+'/../../spec_helper'而不是仅需要'spec_helper'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM