【问题标题】:How to integration test api endpoints of the format 'api/:id' using mocha and supertest如何使用 mocha 和 supertest 集成测试 'api/:id' 格式的 api 端点
【发布时间】:2019-06-20 08:05:27
【问题描述】:

我正在尝试通过将 userId 附加到端点来集成测试 API 端点以返回特定的用户数据

我目前能够测试返回所有用户的端点,但是当我尝试编写一个测试以通过将用户的 id 附加到路由来返回特定用户数据时,我最终只是测试了返回所有用户的路由。

describe('User profile route', () => {

  let token = '';
  let userId = '';
  let userId1 = '';
  useInTest();

  it('should return a specific user details', (done) => {
    signUp(mockData.signUpData).expect(201)
      .end(() => {});
    signUp(mockData.signUpData1).expect(201)
      .end(() => {});
    login(mockData.loginData)
      .expect(200)
      .end((err, res) => {
        token = res.body.accessToken;
        userId = res.body.user._id;
      });

    agent.get(`/api/users/${userId1}`).set('Authorization', 'Bearer ' + token)
      .expect(200)
      .end((err, res) => {
        console.log(res.body);
        res.body.should.have.length(1);
        done();
      })
  });
}

我希望该测试能够通过,但不幸的是,它不仅仅是一直点击这个api/users 而不是点击这个api/users/:id

【问题讨论】:

    标签: express mocha.js supertest


    【解决方案1】:

    我认为这不是一个超级测试问题。您如何使测试异步,因为当您的测试发出请求时,用户 ID 可能是未定义的,因为它是在登录后设置的。尝试像这样更新您的代码(在星号中添加单词):

    it('should return a specific user details', **async**(done) => {
    signUp(mockData.signUpData).expect(201)
      .end(() => {});
    signUp(mockData.signUpData1).expect(201)
      .end(() => {});
    **await** login(mockData.loginData)
      .expect(200)
      .**then**((err, res) => {
        token = res.body.accessToken;
        userId = res.body.user._id;
      });
    
    agent.get(`/api/users/${userId1}`).set('Authorization', 'Bearer ' + token)
      .expect(200)
      .end((err, res) => {
        console.log(res.body);
        res.body.should.have.length(1);
        done();
      })
    

    });

    【讨论】:

    • 这实际上帮助我达到了端点@Herman,但现在我似乎遇到了另一个问题,因为使用异步我需要获取 done() 函数的簧片,当我得到其他一些错误。让我尝试看看我该如何解决这个问题,但请随时分享我能做些什么的想法
    【解决方案2】:

    这就是我设法完全解决问题的方法

    describe('User profile route', () => {
    
      let token = '';
      let userId = '';
      let userId1 = '';
      useInTest();
      beforeEach( async () => {
        signUp(mockData.signUpData).end(() => {});
        signUp(mockData.signUpData1).end(() => {});
        await login(mockData.loginData)
          .then((res) => {
            token = res.body.accessToken;
            userId = res.body.user._id;
          });
        await login(mockData.loginData1)
          .then((res) => {
            userId1 = res.body.user._id;
          });
      });
    
      it('should return user profile for the currently logged in user', (done) => {
        agent.get('/api/users/' + userId).set('Authorization', 'Bearer ' + token)
          .end((err, res) => {
            res.body.should.have.property('name', 'Arnold');
            done();
          });
      });
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 2018-02-12
      • 2015-03-03
      • 1970-01-01
      • 2014-06-17
      • 2016-06-05
      • 1970-01-01
      相关资源
      最近更新 更多