代码之家  ›  专栏  ›  技术社区  ›  jperl

使用嵌套资源测试控制器

  •  0
  • jperl  · 技术社区  · 6 年前

    我是鲁比的新手,我正在慢慢进步。我刚开始测试。

    请注意,我还没有使用任何测试框架,仅仅是rails(5.2.3)提供的现成的框架。

    我有很多书的作者 has_many :books belongs_to :author .

    这些是我的装备:

    图书.yml

    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
    

    作者.yml

    agatha_c:
      name: Agatha Christie
    
    john_s:
      name: John Steinbeck
    

    我跑了

    rails test test/controllers/books_controller_test.rb
    

    但我发现这些测试有错误:

    BooksControllerTest#test_should_update_book
    BooksControllerTest#test_should_show_book
    BooksControllerTest#test_should_get_edit
    BooksControllerTest#test_should_destroy_book
    

    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 <class:BooksControllerTest>'
        test/controllers/books_controller_test.rb:46:in `block in <class:BooksControllerTest>'
    
    

    问题来自于打电话:

    author_book_url id: books(:tmaas).id, author_id: @author.id
    

    edit_author_book_url id: books(:tmaas).id, author_id: @author.id
    
    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
    

    @author 已设置为 setup

    setup do
        @author = authors(:agatha_c)
      end
    

    控制器中的set U book功能:

    def set_book
          @book = @author.books.find(params[:id])
        end
    

    我错过了什么?

    0 回复  |  直到 6 年前
        1
  •  2
  •   Violeta    6 年前

    这就是我通过测试的原因:

    首先,你应该纠正 books.yml

    tmaas:
      title: The Mysterious Affair at Styles
      published: 1920
      author: agatha_c
    
    tgow:
      title: The Grapes of Wrath
      published: 1939
      author: john_s
    

    这是我的测试 book_controller

    1. 创建操作的测试:
      test "should create book" do
        assert_difference('Book.count') do
          post author_books_url(@book.author), params: { book: { 
            author_id: @book.author_id, 
            title: @book.title,
            published: @book.published 
          }}
        end
    
        assert_redirected_to author_book_url(Book.last.author, Book.last)
      end
    
      test "should update book" do
        patch author_book_url(@book.author, @book), params: { book: { 
          author_id: @book.author_id, 
          title: @book.title,
          published: @book.published 
        } }
        assert_redirected_to author_book_url(@book.author, @book)
      end
    
    1. 销毁操作测试:
      test "should destroy book" do
        assert_difference('Book.count', -1) do
          delete author_book_url(@book.author, @book)
        end
    
        assert_redirected_to author_books_url(@book.author)
      end
    

    https://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers

    推荐文章