Reputation: 2961
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
I've tried playing with the namespace definitions, but no luck so far - any ideas???
Upvotes: 1
Views: 5564
Reputation: 31
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
Upvotes: 2
Reputation: 1406
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
Upvotes: 4
Reputation: 116
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
Upvotes: 3
Reputation: 909
You can keep controller under separate folder, but you have to use require File.dirname(FILE) + '/../../spec_helper' instead of just require 'spec_helper'
Upvotes: 0
Reputation: 2961
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
Upvotes: 2