0

Suppose there is an association (bibliography) between writers and books and one of the writers, John Pencil, wrote 16 books. Fixture bibliography.yml lists all the books as follows:

association_1:
    writer: john_pencil
    book: book_1

association_2:
    writer: john_pencil
    book: book_2

... 

I need to test that the home page shows the whole list of J.P.'s books.
Thus if I could use somehow interpolation in the setup method of the test, I could do as follows:

(1..16).each do |num|
    book_#{num} = bibliography("association_#{num}".to_sym).book
end

then I could test that the books list is present as follows:

(1..16).each do |num|
    assert_select "td.book_isdn", book_#{num}.isdn
    assert_select "td.book_title", book_#{num}.title
    assert_select "td.book_genre", book_#{num}.genre
    assert_select "td.book_author", book_#{num}.author  
end

Of course, I cannot use book_#{num} and I am wondering if I can manage with some expedient to make interpolation work as I did for "association_#{num}".to_sym

Asarluhi
  • 1,280
  • 3
  • 22
  • 43

1 Answers1

4

Yeah, don't do that. Instead, create an array of books and use its elements.

books = (1..16).map do |num|
  bibliography("association_#{num}".to_sym).book
end

Then

books.each do |book|
  assert_select "td.book_isdn", book.isdn
  assert_select "td.book_title", book.title
end
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367