问题
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
回答1:
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.
回答2:
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'
回答3:
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.
来源:https://stackoverflow.com/questions/5875423/rspec-response-body-is-still-empty-even-with-config-render-views