Explorar el Código

♻️ refactor(test): 优化广告管理API测试代码结构

- 统一使用index路径访问API端点,提高测试代码一致性
- 将ID参数从字符串类型改为数字类型,匹配API实际参数类型
- 添加空查询参数对象,明确API调用意图
- 修复角色属性格式,添加适当空格
- 为必填字段验证测试添加空值测试数据,增强验证覆盖率
yourname hace 1 mes
padre
commit
b7024fec60

+ 19 - 10
packages/advertisements-module/tests/integration/advertisement-types.integration.test.ts

@@ -36,13 +36,18 @@ describe('广告类型管理API集成测试', () => {
     testToken = JWTUtil.generateToken({
       id: testUser.id,
       username: testUser.username,
-      roles: [{name:'user'}]
+      roles: [{ name: 'user' }]
     });
   });
 
   describe('GET /advertisement-types', () => {
     it('应该返回广告类型列表', async () => {
-      const response = await client.$get({
+      const response = await client.index.$get({
+        query: {
+
+        }
+      },
+      {
         headers: {
           'Authorization': `Bearer ${testToken}`
         }
@@ -59,7 +64,9 @@ describe('广告类型管理API集成测试', () => {
     });
 
     it('应该拒绝未认证用户的访问', async () => {
-      const response = await client.$get();
+      const response = await client.index.$get({
+        query: {}
+      });
       expect(response.status).toBe(401);
     });
   });
@@ -73,7 +80,7 @@ describe('广告类型管理API集成测试', () => {
         status: 1
       };
 
-      const response = await client.$post({
+      const response = await client.index.$post({
         json: createData
       }, {
         headers: {
@@ -96,10 +103,12 @@ describe('广告类型管理API集成测试', () => {
     it('应该验证创建广告类型的必填字段', async () => {
       const invalidData = {
         // 缺少必填字段
+        name: '',
+        code: '',
         remark: '测试备注'
       };
 
-      const response = await client.$post({
+      const response = await client.index.$post({
         json: invalidData
       }, {
         headers: {
@@ -128,7 +137,7 @@ describe('广告类型管理API集成测试', () => {
         status: 1
       };
 
-      const response = await client.$post({
+      const response = await client.index.$post({
         json: duplicateData
       }, {
         headers: {
@@ -154,7 +163,7 @@ describe('广告类型管理API集成测试', () => {
       await advertisementTypeRepository.save(testType);
 
       const response = await client[':id'].$get({
-        param: { id: testType.id.toString() }
+        param: { id: testType.id }
       }, {
         headers: {
           'Authorization': `Bearer ${testToken}`
@@ -174,7 +183,7 @@ describe('广告类型管理API集成测试', () => {
 
     it('应该处理不存在的广告类型', async () => {
       const response = await client[':id'].$get({
-        param: { id: '999999' }
+        param: { id: 999999 }
       }, {
         headers: {
           'Authorization': `Bearer ${testToken}`
@@ -205,7 +214,7 @@ describe('广告类型管理API集成测试', () => {
       };
 
       const response = await client[':id'].$put({
-        param: { id: testType.id.toString() },
+        param: { id: testType.id },
         json: updateData
       }, {
         headers: {
@@ -239,7 +248,7 @@ describe('广告类型管理API集成测试', () => {
       await advertisementTypeRepository.save(testType);
 
       const response = await client[':id'].$delete({
-        param: { id: testType.id.toString() }
+        param: { id: testType.id }
       }, {
         headers: {
           'Authorization': `Bearer ${testToken}`

+ 15 - 8
packages/advertisements-module/tests/integration/advertisements.integration.test.ts

@@ -54,7 +54,9 @@ describe('广告管理API集成测试', () => {
 
   describe('GET /advertisements', () => {
     it('应该返回广告列表', async () => {
-      const response = await client.$get({
+      const response = await client.index.$get({
+        query: {}
+      }, {
         headers: {
           'Authorization': `Bearer ${testToken}`
         }
@@ -71,7 +73,9 @@ describe('广告管理API集成测试', () => {
     });
 
     it('应该拒绝未认证用户的访问', async () => {
-      const response = await client.$get();
+      const response = await client.index.$get({
+        query: {}
+      });
       expect(response.status).toBe(401);
     });
   });
@@ -88,7 +92,7 @@ describe('广告管理API集成测试', () => {
         actionType: 1
       };
 
-      const response = await client.$post({
+      const response = await client.index.$post({
         json: createData
       }, {
         headers: {
@@ -111,10 +115,13 @@ describe('广告管理API集成测试', () => {
     it('应该验证创建广告的必填字段', async () => {
       const invalidData = {
         // 缺少必填字段
+        title: '',
+        typeId: 0,
+        code: '',
         url: 'https://example.com'
       };
 
-      const response = await client.$post({
+      const response = await client.index.$post({
         json: invalidData
       }, {
         headers: {
@@ -144,7 +151,7 @@ describe('广告管理API集成测试', () => {
       await advertisementRepository.save(testAdvertisement);
 
       const response = await client[':id'].$get({
-        param: { id: testAdvertisement.id.toString() }
+        param: { id: testAdvertisement.id }
       }, {
         headers: {
           'Authorization': `Bearer ${testToken}`
@@ -164,7 +171,7 @@ describe('广告管理API集成测试', () => {
 
     it('应该处理不存在的广告', async () => {
       const response = await client[':id'].$get({
-        param: { id: '999999' }
+        param: { id: 999999 }
       }, {
         headers: {
           'Authorization': `Bearer ${testToken}`
@@ -199,7 +206,7 @@ describe('广告管理API集成测试', () => {
       };
 
       const response = await client[':id'].$put({
-        param: { id: testAdvertisement.id.toString() },
+        param: { id: testAdvertisement.id },
         json: updateData
       }, {
         headers: {
@@ -237,7 +244,7 @@ describe('广告管理API集成测试', () => {
       await advertisementRepository.save(testAdvertisement);
 
       const response = await client[':id'].$delete({
-        param: { id: testAdvertisement.id.toString() }
+        param: { id: testAdvertisement.id }
       }, {
         headers: {
           'Authorization': `Bearer ${testToken}`