3

I'm struggling to test whether my select box has a value of 6.

<form class="edit_line_item" id="edit_line_item_7" action="/line_items/7" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="_method" value="patch">
    <select name="line_item[quantity]" id="line_item_quantity"><option selected="selected" value="1">1</option>
    </select>
</form>

My rspec code

assert_select "select#line_item_quantity", value: 6

Whatever I change the value to my test still passes so I am wrong somewhere.

Joe Ainsworth
  • 571
  • 1
  • 6
  • 20

1 Answers1

0

To use assert_select this way you have to pass it a block and do your assertion on the array of elements passed to the block.

assert_select is "An assertion that selects elements and makes one or more equality tests"

Example:

 assert_select "ol" do |elements|
   elements.each do |element|
     assert_select element, "li", 4
   end
 end

You need to tweak this example to assert based on the value attribute of the option tag within the selected element (a select tag in this case).

I recommend reading the docs: http://apidock.com/rails/v2.3.8/ActionController/Assertions/SelectorAssertions/assert_select

If you're using capybara you can use have_select. Answer here: https://stackoverflow.com/a/18622979/156746

Community
  • 1
  • 1
DiegoSalazar
  • 13,361
  • 2
  • 38
  • 55