integration-test-utils.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { describe, it, expect } from 'vitest';
  2. import { IntegrationTestAssertions, ApiResponseAssertions } from '../../src/integration-test-utils';
  3. // 模拟实体用于测试
  4. class MockEntity {
  5. id?: number;
  6. name?: string;
  7. }
  8. describe('IntegrationTestAssertions', () => {
  9. describe('expectStatus', () => {
  10. it('应该在状态码匹配时通过', () => {
  11. const response = { status: 200 };
  12. expect(() => {
  13. IntegrationTestAssertions.expectStatus(response, 200);
  14. }).not.toThrow();
  15. });
  16. it('应该在状态码不匹配时抛出错误', () => {
  17. const response = { status: 404 };
  18. expect(() => {
  19. IntegrationTestAssertions.expectStatus(response, 200);
  20. }).toThrow('Expected status 200, but got 404');
  21. });
  22. });
  23. describe('expectResponseToHave', () => {
  24. it('应该在响应包含特定字段时通过', () => {
  25. const response = { data: { id: 1, name: 'test' } };
  26. const expectedFields = { id: 1, name: 'test' };
  27. expect(() => {
  28. IntegrationTestAssertions.expectResponseToHave(response, expectedFields);
  29. }).not.toThrow();
  30. });
  31. it('应该在字段值不匹配时抛出错误', () => {
  32. const response = { data: { id: 1, name: 'test' } };
  33. const expectedFields = { id: 2, name: 'test' };
  34. expect(() => {
  35. IntegrationTestAssertions.expectResponseToHave(response, expectedFields);
  36. }).toThrow('Expected field id to be 2, but got 1');
  37. });
  38. it('应该在字段不存在时抛出错误', () => {
  39. const response = { data: { id: 1 } };
  40. const expectedFields = { name: 'test' };
  41. expect(() => {
  42. IntegrationTestAssertions.expectResponseToHave(response, expectedFields);
  43. }).toThrow('Expected field name to be test, but got undefined');
  44. });
  45. });
  46. describe('expectResponseStructure', () => {
  47. it('应该在响应包含特定结构时通过', () => {
  48. const response = { data: { id: 1, name: 'test', email: 'test@example.com' } };
  49. const structure = { id: 'number', name: 'string', email: 'string' };
  50. expect(() => {
  51. IntegrationTestAssertions.expectResponseStructure(response, structure);
  52. }).not.toThrow();
  53. });
  54. it('应该在响应缺少字段时抛出错误', () => {
  55. const response = { data: { id: 1, name: 'test' } };
  56. const structure = { id: 'number', name: 'string', email: 'string' };
  57. expect(() => {
  58. IntegrationTestAssertions.expectResponseStructure(response, structure);
  59. }).toThrow('Expected response to have key: email');
  60. });
  61. });
  62. describe('expectErrorResponse', () => {
  63. it('应该在响应包含错误信息时通过', () => {
  64. const response = { data: { error: 'Validation failed' } };
  65. expect(() => {
  66. IntegrationTestAssertions.expectErrorResponse(response, 'Validation');
  67. }).not.toThrow();
  68. });
  69. it('应该在响应不包含错误信息时抛出错误', () => {
  70. const response = { data: { success: true } };
  71. expect(() => {
  72. IntegrationTestAssertions.expectErrorResponse(response, 'Validation');
  73. }).toThrow('Expected error response to contain "Validation"');
  74. });
  75. it('应该在错误信息不匹配时抛出错误', () => {
  76. const response = { data: { error: 'Authentication failed' } };
  77. expect(() => {
  78. IntegrationTestAssertions.expectErrorResponse(response, 'Validation');
  79. }).toThrow('Expected error response to contain "Validation"');
  80. });
  81. });
  82. describe('expectSuccessResponse', () => {
  83. it('应该在成功响应时通过', () => {
  84. const response = { data: { success: true, data: {} } };
  85. expect(() => {
  86. IntegrationTestAssertions.expectSuccessResponse(response);
  87. }).not.toThrow();
  88. });
  89. it('应该在响应包含错误时抛出错误', () => {
  90. const response = { data: { error: 'Something went wrong' } };
  91. expect(() => {
  92. IntegrationTestAssertions.expectSuccessResponse(response);
  93. }).toThrow('Expected success response');
  94. });
  95. it('应该在响应为空时抛出错误', () => {
  96. const response = { data: null };
  97. expect(() => {
  98. IntegrationTestAssertions.expectSuccessResponse(response);
  99. }).toThrow('Expected success response');
  100. });
  101. });
  102. describe('expectListResponseCount', () => {
  103. it('应该在列表响应数量匹配时通过', () => {
  104. const response = { data: [1, 2, 3] };
  105. expect(() => {
  106. IntegrationTestAssertions.expectListResponseCount(response, 3);
  107. }).not.toThrow();
  108. });
  109. it('应该在列表响应数量不匹配时抛出错误', () => {
  110. const response = { data: [1, 2, 3] };
  111. expect(() => {
  112. IntegrationTestAssertions.expectListResponseCount(response, 5);
  113. }).toThrow('Expected 5 items, but got 3');
  114. });
  115. it('应该在响应不是数组时抛出错误', () => {
  116. const response = { data: { items: [1, 2, 3] } };
  117. expect(() => {
  118. IntegrationTestAssertions.expectListResponseCount(response, 3);
  119. }).toThrow('Expected array response');
  120. });
  121. });
  122. describe('expectPaginationResponse', () => {
  123. it('应该在分页响应格式正确时通过', () => {
  124. const response = {
  125. data: {
  126. data: [1, 2, 3],
  127. total: 10,
  128. page: 1,
  129. pageSize: 3
  130. }
  131. };
  132. expect(() => {
  133. IntegrationTestAssertions.expectPaginationResponse(response);
  134. }).not.toThrow();
  135. });
  136. it('应该在缺少分页字段时抛出错误', () => {
  137. const response = {
  138. data: {
  139. data: [1, 2, 3],
  140. total: 10
  141. // 缺少 page 和 pageSize
  142. }
  143. };
  144. expect(() => {
  145. IntegrationTestAssertions.expectPaginationResponse(response);
  146. }).toThrow('Expected pagination response to have field: page');
  147. });
  148. it('应该在数据不是数组时抛出错误', () => {
  149. const response = {
  150. data: {
  151. data: { items: [1, 2, 3] }, // 应该是数组
  152. total: 10,
  153. page: 1,
  154. pageSize: 3
  155. }
  156. };
  157. expect(() => {
  158. IntegrationTestAssertions.expectPaginationResponse(response);
  159. }).toThrow('Expected pagination data to be an array');
  160. });
  161. });
  162. });
  163. describe('ApiResponseAssertions', () => {
  164. describe('expectSuccess', () => {
  165. it('应该在成功响应时通过', () => {
  166. const response = { status: 200, data: { success: true } };
  167. expect(() => {
  168. ApiResponseAssertions.expectSuccess(response);
  169. }).not.toThrow();
  170. });
  171. it('应该在状态码不是200时抛出错误', () => {
  172. const response = { status: 404, data: { success: true } };
  173. expect(() => {
  174. ApiResponseAssertions.expectSuccess(response);
  175. }).toThrow('Expected status 200, but got 404');
  176. });
  177. });
  178. describe('expectCreated', () => {
  179. it('应该在创建响应时通过', () => {
  180. const response = { status: 201, data: { id: 1 } };
  181. expect(() => {
  182. ApiResponseAssertions.expectCreated(response);
  183. }).not.toThrow();
  184. });
  185. });
  186. describe('expectUnauthorized', () => {
  187. it('应该在未授权响应时通过', () => {
  188. const response = { status: 401, data: { error: 'Unauthorized access' } };
  189. expect(() => {
  190. ApiResponseAssertions.expectUnauthorized(response);
  191. }).not.toThrow();
  192. });
  193. });
  194. describe('expectForbidden', () => {
  195. it('应该在禁止访问响应时通过', () => {
  196. const response = { status: 403, data: { error: 'Forbidden access' } };
  197. expect(() => {
  198. ApiResponseAssertions.expectForbidden(response);
  199. }).not.toThrow();
  200. });
  201. });
  202. describe('expectNotFound', () => {
  203. it('应该在未找到响应时通过', () => {
  204. const response = { status: 404, data: { error: 'Resource not found' } };
  205. expect(() => {
  206. ApiResponseAssertions.expectNotFound(response);
  207. }).not.toThrow();
  208. });
  209. });
  210. describe('expectValidationError', () => {
  211. it('应该在验证错误响应时通过', () => {
  212. const response = { status: 400, data: { error: 'Validation failed' } };
  213. expect(() => {
  214. ApiResponseAssertions.expectValidationError(response);
  215. }).not.toThrow();
  216. });
  217. });
  218. });