Tengo un archivo
class Admin::PostsController < ApplicationController layout 'admin' # GET /admin/posts def index @pposts = Post.paginate :page => params[:page], :order => 'created_at DESC' end # ...Many more standard CRUD/REST methods... end
And an rspec test
require 'spec_helper' describe Admin::PostsController do describe "GET 'index'" do it "should be successful" do get 'index' response.should be_success end end #...many more test for all CRUD/REST methods end
However, running that spec throws an error. I have no idea what that error means, nor how to start solving it.
/home/...../active_support/dependencies.rb:492:in `load_missing_constant': Expected /home/...../app/controllers/admin/posts_controller.rb to define Admin::PostsController (LoadError)
I may have it all set up wrong, or may be doing something really silly, but all I want is my CRUD actions on /admin, with separate before filters and a separate layout. And to test these controllers.
EDITAR ZOMG, made a terrible copy-paste error into this SO posting. The controller was PostsController, not the PagesController that I pasted into there. Problem still stands, as my code is correct, just the SO post, here was wrong.
preguntado el 08 de enero de 11 a las 16:01
Everything looks right to me. Are you doing anything unusual in spec_helper?
Your file posts_controller.rb should define the class Admin::PostsController, but it is defining Admin::PagesController.
Was it a copy & paste? :-)
A little bit of trial and error gave me:
describe Admin::PostsController do end
describe "Admin::PostsController" do end
I have no idea why, since the whole Rails module magic is a bit over my head here. But I suspect it is due to the autoloading that does expect a string, and not a class+module here.
EDITAR Allthough this removes the error, it merely introduces a similar problem: @controller is not defined in this case. Making it impossible to test the controller at all :). Stay tuned...
Berkes, you're not supposed to put the controller file in a sub-folder, meaning the files should reside at
Once you get this done, to you need to add a method call "render_views" in you spec just after the "describe Admin::PostsController do" as follows:
require 'spec_helper' describe Admin::PostsController do render_views describe "GET 'index'" do it "should be successful" do get 'index' response.should be_success end end #...many more test for all CRUD/REST methods end