简体   繁体   English

使用Jasmine和Axios进行REST API测试

[英]rest api testing with jasmine and axios

https://blog.kuldeepkamboj.com/node-rest-api-testing-with-jasmine/ I'm already familiar with the above writing unit tests using the request package. https://blog.kuldeepkamboj.com/node-rest-api-testing-with-jasmine/我已经熟悉使用请求包进行的上述编写单元测试。

Currently attempting to write unit tests using Axios https://www.npmjs.com/package/axios . 当前正在尝试使用Axios https://www.npmjs.com/package/axios编写单元测试。

There is no failure when running the spec, but I'm confident that my code is failing silently and its not actually checking the statuscode. 运行规范时没有失败,但是我相信我的代码会以静默方式失败,并且实际上不会检查状态码。 The console.log(response) does not print anything either. console.log(response)也不会打印任何内容。

How can I fix this testcase and was there a better way to debug this? 我该如何修复该测试用例,是否有更好的方法来调试它?

describe('api exists', () => {

it('GET /info should return 200 response', (done) => {
    axios.get("https://somesite.com/info")
    .then((response) => {
        //console.log(response); <-- does not print out anything 
        expect(response.statusCode).toBe(200); 
    })
    done();
});

You can maybe try to place done() inside the promise. 您也许可以尝试将done()放入promise中。

 describe('api exists', () => {

 it('GET /info should return 200 response', (done) => {
   axios.get("https://somesite.com/info")
    .then((response) => {
      console.log(response); 
      expect(response.statusCode).toBe(200); 
      done();
    })
 });

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM