small medium large xlarge


Andrew says:

I added the following to test "should get index in test/functional/products_controller_test.rb:

    assert_select '#product_list'
    assert_select '.list_actions a', :minimum => 3

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.

Phil says:

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

The first 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 get index

It’ll look like this:

  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

Stevie says:

I came up with something like this:

test "should get products index" do
    get :index
    assert_response :success
    assert_select '.products .list_description', minimum: 3
    assert_select '.products .list_description dl dt', 'Programming Ruby 1.9'
    assert_select '.products .list_description dl dd', /.{1,80}/
    assert_select '.products .list_actions', minimum: 3
    ['Show', 'Edit', 'Destroy'].each do |action|
      assert_select '.products .list_actions a', action
    assert_select 'a', 'New Product'

Hi Andrew: I do not understand this line: assert_select '#product_list'

I do not see a div with an id product_list in my app…

Page History
  • V11: Ad Rienks [over 1 year ago]
  • V10: Stevie Braga [about 3 years ago]
  • V9: Stevie Braga [about 3 years ago]
  • V8: Bess Fernandez [over 5 years ago]
  • V7: Bess Fernandez [over 5 years ago]
  • V6: Braulio Carreno [over 6 years ago]
  • V5: Braulio Carreno [over 6 years ago]
  • V4: Braulio Carreno [over 6 years ago]
  • V3: Braulio Carreno [over 6 years ago]
  • V2: Philippe-Antoine Lehoux [over 7 years ago]