Wednesday, January 5, 2011

Rails4 - Iteration D3 - functional tests for new cart button

When building the store page, we went through several examples of adding functional tests to confirm the store index page contained expected page elements. Seemed logical to extend these existing tests to track the addition of the cart button. In doing so learned a couple of interesting features of assert_select

1) use a block to test that each .entry now displays button "Add to Cart"
assert_select '.entry' do
assert_select '.button_to input[value=?]', /Add to Cart/
end

2) check that each form for Add to Cart contains the product_id information, in this case using a substitution marker ? to check the action contents.
assert_select '.entry form[action=?]', /.*product_id=\d+.*/

Note: the substitution regex did not recognize partial matches. For example, /product_id=\d+/ failed to find any elements because 'product_id' is only a partial match to the actual action value /line_items?product_id=4 needed to add .* at start and end to pad out for the remaining action text.

Note for future study: How do I put variables into the regex? for example might be nice to check that the action also contains correct target 'line_items' but I don't really want to hard code that. Instead want to test with line_items_path variable?

No comments:

Post a Comment