即使使用 config.render_views,RSpec response.body 仍然是空的 [英] RSpec response.body is still empty even with config.render_views
问题描述
在我的 spec_helper.rb 文件中,我专门将它设置为 config.render_views 但我返回的 response.body 仍然是空的.这是我的基本规格
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
还有什么可以缩短这种行为的吗?当我通过浏览器时,这很好.我使用的是 Rspec 2.5.0
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
推荐答案
您是否尝试过在控制器规范文件中包含 render_views
?这对我有用.
Have you tried having render_views
in your controller spec file? That works for me.
我注意到的另一件事是,您在测试用例中只能访问索引页面一次 - 准确地说是第一个.其余的将返回空的html内容,因为没有响应.
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.
这就是我将如何实现它.但是如果您在 *spec_helper.rb* 文件中已经有 config.render_views
并且有效,那么您可以在控制器规范中没有 render_views
.
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
这里的细微变化是 before
块,我在其中为每个 it
块调用 get :index
.
The subtle change here is the before
blobk in which I call get :index
for every it
block.
这篇关于即使使用 config.render_views,RSpec response.body 仍然是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!