为什么这个 Capbara 'have_content' 预期意外地通过了?
Why is this Capbara 'have_content' expectation unexpectedly passing?
我正在尝试进行一些验收测试并确认在 Quote#show 页面上呈现的模型的各种属性是否存在。我正在使用如下代码;
expect(page).to have_content("#{quote.industry}")
我正在使用 FactoryGirl Quote.create
和所需的对象属性。 Quote.industry
是在具有一系列选项的引用模型上创建的 enum
,所有工作和渲染都很好。然而,当我在模板上注释掉此属性的显示时,测试仍然通过。页面上没有其他元素匹配 quote.industry。我不知道发生了什么事?
继实际理解上面发生的事情之后,将值插入 Capybara 匹配器参数可能是一种错误的形式?是否应该坚持为 have_content
匹配器提供字符串?
但也许给 have_content 的参数应该只是一个字符串,而不是一个内插的 FactoryGirl 对象。这是可以接受的做法吗?
谢谢
quote_page_spec.rb
require 'rails_helper'
feature 'quote page' do
scenario 'renders all quote attributes' do
quote = FactoryGirl.create(:quote)
visit("/quotes/#{quote.id}")
expect(page).to have_content("#{quote.industry}")
end
end
factories/quotes.rb
FactoryGirl.define do
factory :quote do
sequence(:co_name) { |n| "Acme Co #{n}" }
industry Quote.industries[:financial_services]
end
end
models/quote.rb
class Quote < ApplicationRecord
enum industry: [ :financial_services, :architect, :business_consultancy ]
enum payment_frequency: [ :annually, :monthly ]
end
show.html.erb
<div class="container">
<div class="row">
<div class="col-md-6">
<table class="table table-bordered table-responsive">
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Company name</td>
<td><%= @quote.co_name %></td>
</tr>
<tr>
<td>Company number</td>
<td><%= @quote.co_number %></td>
</tr>
<tr>
<td>Office postcode</td>
<td><%= @quote.postcode %></td>
</tr>
<!-- <tr>
<td>Industry</td>
<td><%= @quote.industry %></td>
</tr> -->
</tbody>
</table>
</div>
</div>
</div>
测试仍然通过,因为内容仍然存在(检查您的页面源)。如果您想注释掉 erb
输出,您必须使用正确的语法,即:
<% =begin %>
<% my_code %>
<%=end %>
用于多行注释和
<%# my_code %>
对于单行。
讨论了这个主题(我的意思是评论,而不是测试)here and here again。
have_content
匹配(默认情况下)进行子字符串匹配。如果 quote.industry
为 nil,它将被插入一个空字符串,该字符串是页面上任何内容的子字符串,因此测试将通过。
我正在尝试进行一些验收测试并确认在 Quote#show 页面上呈现的模型的各种属性是否存在。我正在使用如下代码;
expect(page).to have_content("#{quote.industry}")
我正在使用 FactoryGirl Quote.create
和所需的对象属性。 Quote.industry
是在具有一系列选项的引用模型上创建的 enum
,所有工作和渲染都很好。然而,当我在模板上注释掉此属性的显示时,测试仍然通过。页面上没有其他元素匹配 quote.industry。我不知道发生了什么事?
继实际理解上面发生的事情之后,将值插入 Capybara 匹配器参数可能是一种错误的形式?是否应该坚持为 have_content
匹配器提供字符串?
但也许给 have_content 的参数应该只是一个字符串,而不是一个内插的 FactoryGirl 对象。这是可以接受的做法吗?
谢谢
quote_page_spec.rb
require 'rails_helper'
feature 'quote page' do
scenario 'renders all quote attributes' do
quote = FactoryGirl.create(:quote)
visit("/quotes/#{quote.id}")
expect(page).to have_content("#{quote.industry}")
end
end
factories/quotes.rb
FactoryGirl.define do
factory :quote do
sequence(:co_name) { |n| "Acme Co #{n}" }
industry Quote.industries[:financial_services]
end
end
models/quote.rb
class Quote < ApplicationRecord
enum industry: [ :financial_services, :architect, :business_consultancy ]
enum payment_frequency: [ :annually, :monthly ]
end
show.html.erb
<div class="container">
<div class="row">
<div class="col-md-6">
<table class="table table-bordered table-responsive">
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Company name</td>
<td><%= @quote.co_name %></td>
</tr>
<tr>
<td>Company number</td>
<td><%= @quote.co_number %></td>
</tr>
<tr>
<td>Office postcode</td>
<td><%= @quote.postcode %></td>
</tr>
<!-- <tr>
<td>Industry</td>
<td><%= @quote.industry %></td>
</tr> -->
</tbody>
</table>
</div>
</div>
</div>
测试仍然通过,因为内容仍然存在(检查您的页面源)。如果您想注释掉 erb
输出,您必须使用正确的语法,即:
<% =begin %>
<% my_code %>
<%=end %>
用于多行注释和
<%# my_code %>
对于单行。
讨论了这个主题(我的意思是评论,而不是测试)here and here again。
have_content
匹配(默认情况下)进行子字符串匹配。如果 quote.industry
为 nil,它将被插入一个空字符串,该字符串是页面上任何内容的子字符串,因此测试将通过。