我是Ruby的新手,我正在慢慢取得进步。我刚刚开始测试。
请注意,我尚未使用任何测试框架,仅使用了Rails(5.2.3)提供的功能即可。
我有一些拥有has_many :books
书籍和属于belongs_to :author
作家的书籍的作者。
这些是我的固定装置:
books.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
authors.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_book函数:
def set_book
@book = @author.books.find(params[:id])
end
我想念什么?
答案 0 :(得分:2)
这就是使我的测试通过的原因:
首先,您应该将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
动作的测试:
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
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
查看官方的Rails指南,可能会有所帮助:https://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers