weather.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { createApp, close, createHttpRequest } from '@midwayjs/mock';
  2. import { Framework, Application } from '@midwayjs/koa';
  3. import * as nock from 'nock';
  4. describe('test/controller/weather.test.ts', () => {
  5. let app: Application;
  6. beforeAll(async () => {
  7. // create app
  8. app = await createApp<Framework>();
  9. // 这里由于 github 测试环境无法顺畅的连通国内网络,使用 nock 这个库模拟了服务,实际测试不需要
  10. nock('http://www.weather.com.cn')
  11. .get('/data/sk/101010100.html')
  12. .reply(200, {
  13. weatherinfo: {
  14. city: '北京',
  15. cityid: '101010100',
  16. temp: '27.9',
  17. WD: '南风',
  18. WS: '小于3级',
  19. SD: '28%',
  20. AP: '1002hPa',
  21. njd: '暂无实况',
  22. WSE: '<3',
  23. time: '17:55',
  24. sm: '2.1',
  25. isRadar: '1',
  26. Radar: 'JC_RADAR_AZ9010_JB',
  27. },
  28. });
  29. });
  30. afterAll(async () => {
  31. // close app
  32. await close(app);
  33. nock.restore();
  34. });
  35. it('should test /weather with success request', async () => {
  36. // make request
  37. const result = await createHttpRequest(app)
  38. .get('/weather')
  39. .query({ cityId: 101010100 });
  40. expect(result.status).toBe(200);
  41. expect(result.text).toMatch(/北京/);
  42. });
  43. it('should test /weather with fail request', async () => {
  44. const result = await createHttpRequest(app).get('/weather');
  45. expect(result.status).toBe(200);
  46. expect(result.text).toMatch(/weather data is empty/);
  47. });
  48. });