Rails rspec espera Admin :: PostsController, que está allí

Tengo un archivo app/controllers/admin/posts_controller.rb

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 spec/controllers/admin/posts_controller_spec.rb

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

@zetetic, can you turn this into an answer, it was the spec_helper, with spork-config that caused the problems. Then I can accept that answer. Thanks! -

4 Respuestas

Everything looks right to me. Are you doing anything unusual in spec_helper?

Respondido el 09 de enero de 11 a las 23:01

Your file posts_controller.rb should define the class Admin::PostsController, but it is defining Admin::PagesController.

Was it a copy & paste? :-)

Respondido el 08 de enero de 11 a las 19:01

Yes, but it was a bad copy-paste into the SO post. My code is actually correct. Just confirmed. And again. To not look a fool: again. It is. Will edit question. Sorry. - Berkes

A little bit of trial and error gave me:

describe Admin::PostsController do end

debiera ser

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

Respondido el 09 de enero de 11 a las 18:01

Berkes, you're not supposed to put the controller file in a sub-folder, meaning the files should reside at

app/controllers/posts_controller.rb

y

spec/controllers/posts_controller_spec.rb

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

Respondido el 08 de enero de 11 a las 22:01

Someone -1ed this? Why? I am curious, what is wrong with Tsega's answer? - Berkes

Also, It was rails generate that made these subdirectories. Is running Rails generage controller admin/foo wrong? - Berkes

I don´t know why to vote down, but his answere is not correct once you can, indeed, use subdirs. - Ricardo Acras

Ricardo you might be right, I'm not an expert in rails yet, but I've never seen a rails app with controllers in subdirectories. Berkes just try my suggestion and you will see that it works correctly. - tsega

@tsega: putting controllers in subdirectories is the convention by which Rails implements namespacing, i.e. Admin::PostsController is expected to be defined in app/controllers/admin/posts_controller.rb - zetético

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.