RSpec 控制器测试 - 空白 response.body [英] RSpec controller testing - blank response.body

查看:42
本文介绍了RSpec 控制器测试 - 空白 response.body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 RSpec 测试我的控制器时遇到了一个问题 - response.body 调用总是返回一个空字符串.在浏览器中一切都正确呈现,黄瓜功能测试似乎正确,但 RSpec 每次都失败.

I am stuck with a problem when testing my controllers with RSpec - the response.body call always returns an empty string. In browser everything renders correctly, and cucumber feature tests seem to get it right, but RSpec fails each and every time.

对响应对象的其他期望,例如 response.should render_template('index') 没有任何问题地通过.

Other expectations on the response object, such as response.should render_template('index') pass without any problems.

你们有没有遇到过这个问题?也许可以通过其他方式获得响应html?

Have any of you encountered this problem before? Perhaps the response html can be obtained in some other way?

至于版本,Rails 2.1.0、RSpec 1.2.7.

As for versions, Rails 2.1.0, RSpec 1.2.7.

推荐答案

默认情况下,rspec-rails 侵入 Rails 以阻止它实际渲染视图模板.你应该只测试你的行为的行为 &过滤您的控制器测试,而不是模板渲染的结果 - 这就是视图规范的用途.

By default, rspec-rails hacks into Rails to prevent it from actually rendering view templates. You should only test the behavior of your actions & filters your controller tests, not the outcome of template rendering — that's what view specs are for.

但是,如果您希望像应用程序通常那样使控制器规范呈现模板,请使用 render_views 指令:

However, if you wish to make your controller specs render templates as the app normally would, use the render_views directive:

describe YourController do
  render_views
  ...
end

这篇关于RSpec 控制器测试 - 空白 response.body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆