I added the following to
test "should get index<code>test/functional/products_controller_test.rb</code>:
<code> assert_select '#product_list' assert_select '.list_actions a', :minimum => 3 </code>
The first line checks if the
div with id “product_list” is being displayed. The second line checks if there are at least three actions (show, edit, destroy) being displayed.
I have two concerns with my solution:
1) What happens if there are no items in the catalog? Wouldn’t this test fail?
2) How would I go about implementing this in the future when there are user roles with separate permissions? For example, a regular user should only see “Show”, but an Admin user on the other hand should be able to select “show”, “edit” and “destroy”. This is an important test to have because you don’t want a regression bug to accidentally permit regular users to access super-user functionality.
Andrew, I think it would be ok since you testing against your fixtures. I’m right?
Braulio Carreno says:
Good tests on products_controller, but for the second one I’d prefer checking that each of the
.list_actions elements has 3 actions:
assert_select '.list_actions' do |elements| elements.each do |element| assert_select element, "a", 3 end end
assert_select returns the list of elements with class
.list_actions, you iterate on those to check they contain 3 actions.
About question 2: what we’re doing here is checking the application for public access (i.e. there’s no login involved). When your application has different roles, the functional test has to create a user session before issuing the
It’ll look like this:
UserSession.create(users(:john)) get :index assert_response :success assert_select .....
Bess says:I used this code to check href values. Seems handy and one worth re-using..
test "link hrefs should not be empty for actions" do get :index assert_select "td.list_actions a" do assert_select "[href=?]", /.+/ # Not empty end end