1

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???

4

5 回答 5

4

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

于 2012-06-19T12:24:11.107 回答
3

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
于 2012-06-22T14:01:39.390 回答
2

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
于 2014-03-20T12:30:01.643 回答
1

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  
于 2010-12-21T10:48:37.107 回答
0

You can keep controller under separate folder, but you have to use require File.dirname(FILE) + '/../../spec_helper' instead of just require 'spec_helper'

于 2011-06-20T19:23:43.043 回答