Browse Source

✅ test(files): add service error handling test case

- 恢复并完善文件上传策略接口的服务错误处理测试
- 添加path字段到测试数据以符合接口要求
- 使用vi.mocked正确模拟FileService依赖
- 验证服务抛出错误时接口返回500状态码
yourname 2 months ago
parent
commit
fb65b479e5
1 changed files with 24 additions and 20 deletions
  1. 24 20
      src/server/api/files/__tests__/files.integration.test.ts

+ 24 - 20
src/server/api/files/__tests__/files.integration.test.ts

@@ -135,26 +135,30 @@ describe('File API Integration Tests', () => {
       expect(response.status).toBe(400);
     });
 
-    // it('should handle service errors gracefully', async () => {
-    //   const mockFileData = {
-    //     name: 'test.txt',
-    //     type: 'text/plain',
-    //     uploadUserId: 1
-    //   };
-
-    //   mockFileService.createFile = vi.fn().mockRejectedValue(new Error('Service error'));
-
-    //   const response = await client.files['upload-policy'].$post({
-    //     json: mockFileData
-    //   },
-    //   {
-    //     headers: {
-    //       'Authorization': 'Bearer test-token'
-    //     }
-    //   });
-
-    //   expect(response.status).toBe(500);
-    // });
+    it('should handle service errors gracefully', async () => {
+      const mockFileData = {
+        name: 'test.txt',
+        type: 'text/plain',
+        path: '/uploads/test.txt',
+        uploadUserId: 1
+      };
+
+      const mockCreateFile = vi.fn().mockRejectedValue(new Error('Service error'));
+      vi.mocked(FileService).mockImplementation(() => ({
+        createFile: mockCreateFile
+      } as unknown as FileService));
+
+      const response = await client.files['upload-policy'].$post({
+        json: mockFileData as any
+      },
+      {
+        headers: {
+          'Authorization': 'Bearer test-token'
+        }
+      });
+
+      expect(response.status).toBe(500);
+    });
   });
 
   // describe('GET /api/v1/files/{id}/url', () => {