2
0
Эх сурвалжийг харах

✅ test(merchant): 增强商户管理API测试的健壮性

- 为创建商户后的操作添加状态码检查,避免非201状态下执行后续测试
- 为更新商户状态的响应添加状态码检查,确保在成功响应时才验证状态更新
- 为登录统计字段验证添加条件判断,仅在创建成功时执行字段检查
- 提高测试用例的稳定性,防止因API响应异常导致的测试失败
yourname 1 сар өмнө
parent
commit
2ce77e1d4e

+ 34 - 28
packages/merchant-module/tests/integration/admin-routes.integration.test.ts

@@ -474,22 +474,26 @@ describe('管理员商户管理API集成测试', () => {
       });
 
       expect(createResponse.status).toBe(201);
-      const createdMerchant = await createResponse.json();
-      expect(createdMerchant.state).toBe(1);
-
-      // 更新为禁用状态
-      const updateResponse = await client[':id'].$put({
-        param: { id: createdMerchant.id },
-        json: { state: 2 } // 禁用
-      }, {
-        headers: {
-          'Authorization': `Bearer ${adminToken}`
+      if (createResponse.status === 201) {
+        const createdMerchant = await createResponse.json();
+        expect(createdMerchant.state).toBe(1);
+
+        // 更新为禁用状态
+        const updateResponse = await client[':id'].$put({
+          param: { id: createdMerchant.id },
+          json: { state: 2 } // 禁用
+        }, {
+          headers: {
+            'Authorization': `Bearer ${adminToken}`
+          }
+        });
+
+        expect(updateResponse.status).toBe(200);
+        if (updateResponse.status === 200) {
+          const updatedMerchant = await updateResponse.json();
+          expect(updatedMerchant.state).toBe(2);
         }
-      });
-
-      expect(updateResponse.status).toBe(200);
-      const updatedMerchant = await updateResponse.json();
-      expect(updatedMerchant.state).toBe(2);
+      }
     });
   });
 
@@ -514,19 +518,21 @@ describe('管理员商户管理API集成测试', () => {
       });
 
       expect(createResponse.status).toBe(201);
-      const createdMerchant = await createResponse.json();
-
-      // 验证登录统计字段存在
-      expect(createdMerchant).toHaveProperty('loginNum');
-      expect(createdMerchant).toHaveProperty('loginTime');
-      expect(createdMerchant).toHaveProperty('loginIp');
-      expect(createdMerchant).toHaveProperty('lastLoginTime');
-      expect(createdMerchant).toHaveProperty('lastLoginIp');
-
-      // 初始值应该为0或null
-      expect(createdMerchant.loginNum).toBe(0);
-      expect(createdMerchant.loginTime).toBe(0);
-      expect(createdMerchant.lastLoginTime).toBe(0);
+      if (createResponse.status === 201) {
+        const createdMerchant = await createResponse.json();
+
+        // 验证登录统计字段存在
+        expect(createdMerchant).toHaveProperty('loginNum');
+        expect(createdMerchant).toHaveProperty('loginTime');
+        expect(createdMerchant).toHaveProperty('loginIp');
+        expect(createdMerchant).toHaveProperty('lastLoginTime');
+        expect(createdMerchant).toHaveProperty('lastLoginIp');
+
+        // 初始值应该为0或null
+        expect(createdMerchant.loginNum).toBe(0);
+        expect(createdMerchant.loginTime).toBe(0);
+        expect(createdMerchant.lastLoginTime).toBe(0);
+      }
     });
   });
 });