Ruby-on-rails 如何使用 RSpec 测试渲染部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9946502/
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
How to test rendering a partial with RSpec
提问by Pavel
I want to test rendering a particular partial according to some conditions.
我想根据某些条件测试渲染特定的部分。
For example, in model show action view show.html.erbI have:
例如,在模型显示动作视图中,show.html.erb我有:
<% if condition1 %>
<%= render :partial => "partial1" %>
<% else %>
<%= render :partial => "partial1" %>
<% end %>
I tried:
我试过:
response.should render_template("partial_name")
but it tells that it rendered "show" template
但它告诉它呈现“显示”模板
expecting <"partial1"> but rendering with <"model/show, layouts/application">
期待 <"partial1"> 但使用 <"model/show, layouts/application"> 渲染
What I am doing wrong?
我做错了什么?
回答by Rishav Rastogi
Also try this
也试试这个
response.should render_template(:partial => 'partial_name')
回答by swilgosz
Latest rspec version suggest to use expectsyntax rather than should:
最新的 rspec 版本建议使用expect语法而不是should:
expect(response).to render_template(partial: 'partial_name')
回答by Diego D
If you are testing this inside a controlleryou should do something like this:
如果您在控制器中测试它,您应该执行以下操作:
RSpec.describe Users::RegistrationsController, type: :controller do
describe "GET #new" do
render_views
it "render customer partial" do
get :new
expect(response).to render_template :new
expect(response).to render_template(partial: '_new_customer')
end
end
end
Note that we need render_viewsas reported into documentation.
请注意,我们需要在文档中报告的render_views。
And this is the line that will test if "_new_customer" partial is rendered:
这是将测试是否呈现“_new_customer”部分的行:
expect(response).to render_template(partial: '_new_customer')
You need to provide the name of the partial with the initial underscore.
您需要提供带有初始下划线的部分名称。
Also be careful because in your code the IF and the ELSE statements are rendering the same thing.
还要小心,因为在您的代码中,IF 和 ELSE 语句呈现相同的内容。
回答by user2238766
If using in rspec controllers
如果在 rspec 控制器中使用
expect(response).to render_template(partial: 'home/_sector_performance')
回答by Dende
回答by coorasse
Starting from Rails 5.1, this kind of test is discouraged and you should test the controller and the view as a whole.
从 Rails 5.1 开始,不鼓励进行这种测试,您应该将控制器和视图作为一个整体进行测试。
Checking which partial is rendered by the controlled is part of the implementation details you shouldn't test.
检查哪个部分由受控呈现是您不应该测试的实现细节的一部分。
Therefore I suggest you to write a request test and check that some relevant text of your partial is present in the response body.
因此,我建议您编写一个请求测试并检查响应正文中是否存在您部分的一些相关文本。
get root_path
expect(CGI.unescape_html(response.body)).to include('Hello World')
回答by auralbee
Instead of the above mentioned solution you could check alternatively, if the html that the partial renders is present. E.g.
除了上述解决方案,您还可以检查部分呈现的 html 是否存在。例如
response.body.should have_tag("div#foo")

