即使使用 config.render_views,RSpec response.body 仍然为空
在我的spec_helper.rb 文件中,我专门将其设置为config.render_views,但我返回的response.body 仍然是空的。这是我的基本规格
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过在控制器规范文件中包含
render_views
?这对我有用。我注意到的另一件事是,您在测试用例中只访问索引页一次 - 准确地说是第一个。其余的会因为没有响应而返回空的html内容。
这就是我将如何实现它。但是,如果 *spec_helper.rb* 文件中已有 config.render_views 且可以正常工作,则可以在控制器规范中不使用 render_views。
编辑:
这里的微妙变化是在
before
blobk 中,我为每个it
块调用get :index
。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 therender_views
in the controller spec.EDIT:
The subtle change here is the
before
blobk in which I callget :index
for everyit
block.我也遇到过同样的问题。
解决方案是指定请求的格式。
例如:
<代码>
获取:some_action,some_param:12121,格式:'json'
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'
这已从 RSpec 1 更改为 RSpec 2。视图规范现在使用
rendered
而不是response
:rendered.should =~ /some text/
更多github 上的发行说明中的信息。
This was changed from RSpec 1 to RSpec 2. View specs now use
rendered
instead ofresponse
:rendered.should =~ /some text/
More info in the release notes on github.