コントローラ毎の各アクションが、仕様通りに動作するかをテストします。
テストコードは、test “テスト名” do ~ end の書式です。
一例:
test "should not destroy book" do
assert_no_difference "Book.count" do
delete book_url(@book)
end
assert_response :unprocessable_entity
end
assert_no_difference は、
このブロック内の実行によって、
対象の数 (例では “Book.count”) が変わらない事を確認します。
サンプルプログラムでは、book は削除できないので、数は変わらないハズです。
逆に、
assert_difference は、数が変わることを確認します。
期待される差が 1 の場合は、assert_difference(“対象”) do ~ end、
1以外の場合は、assert_difference(“対象”, 変化の値) do ~ end、
です。
assert_response は、
実行後の HTTP Status Code が指定のものかを確認します。
直接数値を書いてもいいですし、例のようにラベルも使えます。
他にも assert 系は沢山あるので、
必要があれば、Rails ドキュメント を確認してください。
参考リポジトリ: https://github.com/Bonv-dev/book_mgmt/commit/0bf3f2d の
\test\controllers\books_controller_test.rb など。
書式は、controllers と同じです。
というか、controllers のほうが integration を借用しています。