Ruby-on-rails RSpec 控制器测试 - 空白 response.body

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1063073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:24:28  来源:igfitidea点击:

RSpec controller testing - blank response.body

ruby-on-railsrubyunit-testingrspec

提问by Toms Mikoss

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.

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

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

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

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

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

As for versions, Rails 2.1.0, RSpec 1.2.7.

至于版本,Rails 2.1.0,RSpec 1.2.7。

回答by mislav

By default, rspec-rails hacks into Rails to preventit 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.

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

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

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

describe YourController do
  render_views
  ...
end

回答by Thomas

RSpec 2+: If you want to check end to end - url to response body - use a request specinstead of a controller spec.

RSpec 2+:如果你想检查端到端 - url 到响应正文 - 使用请求规范而不是控制器规范

回答by John Lockwood

As I worked with a similar problem (that led me to this question), it occurred to me that there are different ways to skin the same cat. In other words, rather than checking for the body text, you might be able to check the content of the flash.

当我处理一个类似的问题时(这让我想到了这个问题),我突然想到有不同的方法可以给同一只猫剥皮。换句话说,您可以检查 flash 的内容,而不是检查正文文本。

response.body.should =~ /Invalid email or password/

might be an equivalent check to:

可能是等效的检查:

flash[:alert].should == "Invalid email or password"

To me the latter seems a bit more flexible as it will run either way, but it may not be appropriate in all cases.

对我来说,后者似乎更灵活,因为它可以以任何一种方式运行,但它可能并不适用于所有情况。

Cheers,

干杯,

John

约翰

回答by Nesha Zoric

By default, RSpec-rails configuration disables rendering of templates for controller specs

默认情况下,RSpec-rails 配置禁用控制器规范的模板渲染

One of the ways to fix this is by making sure to enable the render_viewssetting in your rails_helper.rbfile. In this way, you make it able to work globally in all your tests.

解决此问题的方法之一是确保启用文件中的render_views设置rails_helper.rb。通过这种方式,您可以在所有测试中全局工作。

RSpec.configure do |config|
  config.render_views
end

Or use render_views declaration an individual group:

或者使用 render_views 声明一个单独的组:

describe User do
  render_views
end

You can read more about this here.

您可以在此处阅读更多相关信息。