作为我的 View 规范的一部分,我有时喜欢检查页面上的某个元素是否也呈现了正确的文本。
假设渲染的 HTML 是 -
<div class="user-msg">
<b>Thank You!</b>
</div>
我可能会在我的 View 规范中进行测试 -
expect(rendered).to match(/Thank You/)
但是,这可能会错误地失败或通过,具体取决于页面上是否还有其他内容也说“谢谢”。
如何在特定范围内搜索
<div>
有问题?如果这是一个功能测试,我会使用 Capybara 来选择节点 -within(".user-mssg") { expect(page.body).to have_content("Thank You") }
如何找到特定的 HTML 元素(例如
.user-msg
)并在我的 View 规范中在其中进行搜索?谢谢!
编辑:“谢谢!”
最佳答案
rspec-html-matchers就是这样:
<h1>Simple Form</h1>
<form action="/users" method="post">
<p>
<input type="email" name="user[email]" />
</p>
<p>
<input type="submit" id="special_submit" />
</p>
</form>
规范:
expect(rendered).to have_tag('form', :with => { :action => '/users', :method => 'post' }) do
with_tag "input", :with => { :name => "user[email]", :type => 'email' }
with_tag "input#special_submit", :count => 1
without_tag "h1", :text => 'unneeded tag'
without_tag "p", :text => /content/i
end
关于ruby-on-rails-4 - 在 RSpec View 测试中的 HTML 元素中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510338/