jonleighton / focused_controller

MIT License
467 stars 27 forks source link

Cannot set controller class in RSpec #1

Closed jonleighton closed 12 years ago

jonleighton commented 12 years ago

It should be possible to override the described class via self.controller_class = Foo.

dyba commented 12 years ago

Do you have a code snippet you can share to show the use case? I imagine I may bump into this since I'm using RSpec.

jonleighton commented 12 years ago

IIRC, I think it's just if you want to have a free-text description block. e.g.

describe 'foo' do
  self.controller_class = LolController::Show
end

vs

describe LolController::Show do
end
dyba commented 12 years ago

TL;DR Yup, I could use that.

Oh that's what your talking about! What I did notice is I have to type the controller class over and over for each nested describe block:

describe ProjectsController do
  include FocusedController::RSpecHelper

  describe ProjectsController::Index do
    it "should be successful" do
      get :index
      response.should be_successful
    end
  end

  describe ProjectsController::New do
    it "should be successful" do
      get :new
      response.should be_successful
    end
  end

  describe ProjectsController::Create do
    before(:each) do
      @project = { name: "Project1", description: "Description1" }
    end

    it "should redirect to the projects page" do
      post :create, params: @project
      response.should redirect_to(projects_url)
    end
  end

It would be nice if I don't have to type ProjectsController again after the first describe.

describe ProjectsController do
  include FocusedController::RSpecHelper

  describe Index do
    it "should be successful" do
      get :index
      response.should be_successful
    end
  end

  describe New do
    it "should be successful" do
      get :new
      response.should be_successful
    end
  end

  describe Create do
    before(:each) do
      @project = { name: "Project1", description: "Description1" }
    end

    it "should redirect to the projects page" do
      post :create, params: @project
      response.should redirect_to(projects_url)
    end
end

Then again if I manage to get that working, I don't think there'd be much value to that as you'll see below:

$ rake spec:controllers

ProjectsController
  Index
    should be successful
  New
    should be successful
  Create
    should redirect to the projects page

Running RSpec tests in documentation format don't provide much description about what you are testing (i.e. was that a POST or GET request test?)

There's more value to your use case. The examples would be self-descriptive:

$ rake spec:controllers

ProjectsController
  GET 'index'
    should be successful
  GET 'new'
    should be successful
  POST 'create'
    should redirect to the projects page
dyba commented 12 years ago

Not sure what the issue is now. The following works fine without a problem:

describe ProjectsController do
  include FocusedController::RSpecHelper

  describe "GET 'index'" do
    self.controller_class = ProjectsController::Index

    it "should be successful" do
      get :index
      response.should be_successful
    end
  end

  describe "GET 'new'" do
    self.controller_class = ProjectsController::New

    it "should be successful" do
      get :new
      response.should be_successful
    end
  end
end

Here are my test results:

$ rake spec:controllers

ProjectsController
  GET 'index'
    should be successful
  GET 'new'
    should be successful
dyba commented 12 years ago

What version of RSpec are you using? I have 2.9.0.

jonleighton commented 12 years ago

Maybe I was mistaken and there is not a problem. This can just be a note for me to investigate :)