Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec response.body is still empty even with config.render_views

In my spec_helper.rb file I have specifically set it to config.render_views but the response.body I get back is still empty. Here is my basic spec

describe  "#index" do
    it "should list all rooms" do
      get 'index'
      stub(Person).all
    end

    it "responds with 200 response code" do
      response.should be_ok
    end

    it "renders the index template" do
      pp response.body
      response.should render_template("people/index")
    end

  end

Is there anything else that could have shorted this behavior? It's fine when I go through the browser. I am on Rspec 2.5.0

like image 588
CountCet Avatar asked May 03 '11 20:05

CountCet


1 Answers

Have you tried having render_views in your controller spec file? That works for me.

Another thing I noticed is that you only access the index page once in your test cases - the first one to be precise. The rest will return empty html content because there is no response.

This is how I will implement it. But if you already have config.render_views in the *spec_helper.rb* file and that works, you can do without the render_views in the controller spec.

describe MyController
    render_views

    before :each do
        get :index
    end

    describe  "#index" do
        it "should list all rooms" do
            stub(Person).all
        end

        it "responds with 200 response code" do
            response.should be_ok
        end

        it "renders the index template" do
            pp response.body
            response.should render_template("people/index")
        end
    end
end

EDIT: The subtle change here is the before blobk in which I call get :index for every it block.

like image 90
Igbanam Avatar answered Sep 28 '22 13:09

Igbanam