title: Testing controllers with nested resources
body: <p>I'm new to Ruby and I'm slowly making progress. I've just gotten to testing.</p>
  
  <p>Please note that I'm not yet using any testing framework, merely what rails (5.2.3) provides out of the box.</p>
  
  <p>I have authors who have many books <code>has_many :books</code> and books that belong to authors <code>belongs_to :author</code>.</p>
  
  <p>These are my fixtures:</p>
  
  <p>books.yml</p>
  
  <pre><code>tmaas:
    name: The Mysterious Affair at Styles
    published: 1920
    author_id: agatha_c
  
  tgow:
    name: The Grapes of Wrath
    published: 1939
    author_id: john_s
  </code></pre>
  
  <p>authors.yml</p>
  
  <pre><code>agatha_c:
    name: Agatha Christie
  
  john_s:
    name: John Steinbeck
  </code></pre>
  
  <p>I ran</p>
  
  <pre><code>rails test test/controllers/books_controller_test.rb
  </code></pre>
  
  <p>but I'm getting errors for these tests:</p>
  
  <pre><code>BooksControllerTest#test_should_update_book
  BooksControllerTest#test_should_show_book
  BooksControllerTest#test_should_get_edit
  BooksControllerTest#test_should_destroy_book
  </code></pre>
  
  <p>The error is always the same, it can't find the book.</p>
  
  <pre><code>Error:
  BooksControllerTest#test_should_destroy_book:
  ActiveRecord::RecordNotFound: Couldn't find Book with 'id'=445166326 [WHERE "books"."author_id" = ?]
      app/controllers/books_controller.rb:72:in `set_book'
      test/controllers/books_controller_test.rb:47:in `block (2 levels) in &lt;class:BooksControllerTest&gt;'
      test/controllers/books_controller_test.rb:46:in `block in &lt;class:BooksControllerTest&gt;'
  
  </code></pre>
  
  <p>The problem comes from calling:</p>
  
  <pre><code>author_book_url id: books(:tmaas).id, author_id: @author.id
  </code></pre>
  
  <p>or </p>
  
  <pre><code>edit_author_book_url id: books(:tmaas).id, author_id: @author.id
  </code></pre>
  
  <pre><code>test "should destroy book" do
      assert_difference('Book.count', -1) do
        delete author_book_url id: books(:tmaas).id, author_id: @author.id
      end
  
      assert_redirected_to author_books_url(@author)
    end
  </code></pre>
  
  <p><code>@author</code> is set in <code>setup</code></p>
  
  <pre><code>setup do
      @author = authors(:agatha_c)
    end
  </code></pre>
  
  <p>The set_book function in the controller:</p>
  
  <pre><code>def set_book
        @book = @author.books.find(params[:id])
      end
  </code></pre>
  
  <p>What am I missing?</p>