“assert_select”如何在Rails中起作用

时间:2013-11-11 22:30:22

标签: ruby-on-rails testing

我在Rails应用程序中尝试了以下测试:

test "markup need for store.js.coffee is in place" do
  get :index
  assert_select '.store .entry > img', 3
  assert_select '.entry input[type=submit]', 3
end

测试一切正常,我没有失败或错误,但我无法理解assert_selects在我的HTML中寻找什么。
我将尝试以更好的方式解释自己:assert_select '.entry input[type=submit]', 3是否在.entry元素内寻找输入类型=提交的确切3个字段?什么是第一个asser_select寻找?

这是assert_selects行为的HTML

<body class="store">
<div id="columns">
<div id="main">
<h1>Your Pragmatic Catalog</h1>

<div class="entry">
<img height="95px" src="/assets/cs.jpg" alt="Cs">
<h3>CoffeeScript</h3>
<p> CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality                 wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code. </p>
<div class="price_line">
<span class="price">$36.00</span>
<form class="button_to" method="post" data-remote="true" action="/line_items?product_id=2">
<div>
<input type="submit" value="Add to Cart">
<input type="hidden" value="H5APP93C0onJsfliaMRqww+ER0u/hTZAjvGIeMluHIo=" name="authenticity_token">
</div>
</form>
</div>
</div>

<div class="entry">
<img height="95px" src="/assets/hp.jpg" alt="Hp">
<h3>Harry Potter</h3>
<p>Mago</p>
<div class="price_line">
<span class="price">$15.00</span>
<form class="button_to" method="post" data-remote="true" action="/line_items?product_id=5">
<div>
<input type="submit" value="Add to Cart">
<input type="hidden" value="H5APP93C0onJsfliaMRqww+ER0u/hTZAjvGIeMluHIo=" name="authenticity_token">
</div>
</form>
</div>
</div>

<div class="entry">
<img height="95px" src="/assets/ruby.jpg" alt="Ruby">
<h3>Programming Ruby 1.9 & 2.0</h3>
<p> Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox. </p>
<div class="price_line">
<span class="price">$49.95</span>
<form class="button_to" method="post" data-remote="true" action="/line_items?product_id=3">
<div>
<input type="submit" value="Add to Cart">
<input type="hidden" value="H5APP93C0onJsfliaMRqww+ER0u/hTZAjvGIeMluHIo=" name="authenticity_token">
</div>
</form>
</div>
</div>

<div class="entry">
<img height="95px" src="/assets/rtp.jpg" alt="Rtp">
<h3>Rails Test Prescriptions</h3>
<p>
<em>Rails Test Prescriptions</em>
is a comprehensive guide to testing Rails applications, covering Test-Driven Development    from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.
</p>
<div class="price_line">
<span class="price">$34.95</span>
<form class="button_to" method="post" data-remote="true" action="/line_items?product_id=4">
<div>
<input type="submit" value="Add to Cart">
<input type="hidden" value="H5APP93C0onJsfliaMRqww+ER0u/hTZAjvGIeMluHIo=" name="authenticity_token">
</div>
</form>
</div>
</div>
</div>
</div>
</body>

2 个答案:

答案 0 :(得分:2)

好问题。我正在编写同一本书 - 使用Rails 4进行敏捷Web开发。这是件好事。

回答你的问题:

第一个assert_select正在查找3个图像元素,这些图像元素是.entry元素的直接子元素,它位于dom树上的.store元素下面。

第二个是,正如你所建议的那样,在dom树上寻找属于submit的3个输入字段,这些字段位于.entry元素下面。

答案 1 :(得分:0)

第一个选择是使用.entry类查找作为元素的第一个子元素的img,它是元素的后代.store

http://css-tricks.com/child-and-sibling-selectors/