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

1
  • 3
    Is there any reason you can't just use an array? Commented Jan 20, 2017 at 16:56

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.