|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require "test_helper" |
2 | 4 |
|
3 | 5 | class CategoriesControllerTest < ActionDispatch::IntegrationTest |
| 6 | + setup do |
| 7 | + @category = categories(:one) |
| 8 | + sign_in_as users(:one) |
| 9 | + end |
| 10 | + |
4 | 11 | test "should get index" do |
5 | | - get categories_index_url |
| 12 | + get categories_url |
6 | 13 | assert_response :success |
7 | 14 | end |
8 | 15 |
|
9 | 16 | test "should get new" do |
10 | | - get categories_new_url |
| 17 | + get new_category_url |
11 | 18 | assert_response :success |
12 | 19 | end |
13 | 20 |
|
14 | | - test "should get create" do |
15 | | - get categories_create_url |
| 21 | + test "should create category" do |
| 22 | + assert_difference("Category.count") do |
| 23 | + post categories_url, params: {category: {name: "New Category"}} |
| 24 | + end |
| 25 | + |
| 26 | + assert_redirected_to categories_url |
| 27 | + end |
| 28 | + |
| 29 | + test "should show category" do |
| 30 | + get category_url(@category) |
16 | 31 | assert_response :success |
17 | 32 | end |
18 | 33 |
|
19 | 34 | test "should get edit" do |
20 | | - get categories_edit_url |
| 35 | + get edit_category_url(@category) |
21 | 36 | assert_response :success |
22 | 37 | end |
23 | 38 |
|
24 | | - test "should get update" do |
25 | | - get categories_update_url |
26 | | - assert_response :success |
| 39 | + test "should update category" do |
| 40 | + patch category_url(@category), params: {category: {name: "Updated Category"}} |
| 41 | + assert_redirected_to categories_url |
27 | 42 | end |
28 | 43 |
|
29 | | - test "should get destroy" do |
30 | | - get categories_destroy_url |
31 | | - assert_response :success |
| 44 | + test "should destroy category" do |
| 45 | + # Create a category that is not associated with any budget or transaction |
| 46 | + category_to_destroy = Category.create!(name: "To be destroyed") |
| 47 | + assert_difference("Category.count", -1) do |
| 48 | + delete category_url(category_to_destroy) |
| 49 | + end |
| 50 | + |
| 51 | + assert_redirected_to categories_url |
32 | 52 | end |
33 | 53 | end |
0 commit comments