Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 在RSpec视图测试中搜索HTML元素_Ruby On Rails 4_Rspec_Views - Fatal编程技术网

Ruby on rails 4 在RSpec视图测试中搜索HTML元素

Ruby on rails 4 在RSpec视图测试中搜索HTML元素,ruby-on-rails-4,rspec,views,Ruby On Rails 4,Rspec,Views,作为我的视图规范的一部分,我有时喜欢检查页面上的某个元素是否也呈现了正确的文本 假设呈现的HTML是- <div class="user-msg"> <b>Thank You!</b> </div> 然而,这可能会错误地失败或通过,这取决于页面上是否还有其他内容也写着“谢谢” 如何在相关的特定中进行搜索?如果这是一个功能测试,我会使用水豚来选择节点- within(".user-mssg") { expect(page.body).to ha

作为我的视图规范的一部分,我有时喜欢检查页面上的某个元素是否也呈现了正确的文本

假设呈现的HTML是-

<div class="user-msg">
  <b>Thank You!</b>
</div>
然而,这可能会错误地失败或通过,这取决于页面上是否还有其他内容也写着“谢谢”

如何在相关的特定
中进行搜索?如果这是一个功能测试,我会使用水豚来选择节点-

within(".user-mssg") { expect(page.body).to have_content("Thank You") }
如何查找特定的HTML元素(例如,
.user msg
),并在我的视图规范中进行搜索

谢谢

编辑:“谢谢!”

就是这样做的:

<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>
<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