12

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

CountCet
  • 4,545
  • 7
  • 30
  • 39

3 Answers3

8

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.

Igbanam
  • 5,904
  • 5
  • 44
  • 68
  • Did you fix your code by including `get :index` into every `it` block? – Igbanam May 19 '11 at 00:04
  • 1
    You certainly need `get :index` in every test, or better, in a `before` as @Yasky has shown. Your `stub` is also doing nothing in the test it's in. Stubbing after the `get` is going to have no effect. You're also not returning anything from the stub. – Jim Stewart Jun 07 '13 at 16:55
3

I've had the same issue.

The solution was to specify the format of the request.

For example: get :some_action, some_param: 12121, format: 'json'

viniciux
  • 105
  • 1
  • 1
  • 7
0

This was changed from RSpec 1 to RSpec 2. View specs now use rendered instead of response:

rendered.should =~ /some text/

More info in the release notes on github.

zetetic
  • 47,184
  • 10
  • 111
  • 119
  • I'm not in a view spec, it's a controller spec and the docs still show dealing with a response in this fashion – CountCet May 17 '11 at 22:07