ソースを参照

Add def update and unit tests

Lou 3年前
コミット
da5edf8eab

+ 1
- 1
app/controllers/api/v1/activities_controller.rb ファイルの表示

@@ -25,7 +25,7 @@ class Api::V1::ActivitiesController < ApplicationController
25 25
 
26 26
   def update
27 27
     if @activity.update(activity_params)
28
-      render json: ActivitySerializer.new(@activity).serializable_hash.to_json
28
+      render json: ActivitySerializer.new(@activity).serializable_hash.to_json, status: :ok
29 29
     else
30 30
       render json: @activity.errors, status: :unprocessable_entity
31 31
     end

+ 8
- 0
app/controllers/api/v1/tasks_controller.rb ファイルの表示

@@ -21,6 +21,14 @@ class Api::V1::TasksController < ApplicationController
21 21
     end
22 22
   end
23 23
 
24
+  def update
25
+    if @task.update(task_params)
26
+      render json: TaskSerializer.new(@task).serializable_hash.to_json, status: :ok
27
+    else
28
+      render json: @task.errors, status: :unprocessable_entity
29
+    end
30
+  end
31
+
24 32
   private
25 33
 
26 34
   def task_params

+ 17
- 1
test/controllers/api/v1/tasks_controller_test.rb ファイルの表示

@@ -51,7 +51,7 @@ class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
51 51
     assert_response :created
52 52
   end
53 53
 
54
-  test "should forbid create task" do
54
+  test "should not create task" do
55 55
     assert_no_difference("Task.count") do
56 56
       post api_v1_activity_tasks_url(@activity),
57 57
       params: { task: { name: @task.name, description: @task.description, user_id: @user, activity_id: @activity } },
@@ -59,4 +59,20 @@ class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
59 59
     end
60 60
     assert_response :forbidden
61 61
   end
62
+
63
+  # UPDATE
64
+  test "should update task" do
65
+    patch api_v1_activity_task_url(@activity, @task),
66
+    params: { task: { name: "New name", description: "New description" } },
67
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
68
+    as: :json
69
+    assert_response :success
70
+  end
71
+
72
+  test "should not update task" do
73
+    patch api_v1_activity_task_url(@activity, @task),
74
+    params: { task: { name: "New name", description: "New description" } },
75
+    as: :json
76
+    assert_response :forbidden
77
+  end
62 78
 end

読み込み中…
キャンセル
保存