无法将参数从api请求传递到外部

问题描述 投票:0回答:1

下面的代码是一个简单的摩卡测试,我试图传递变量my_token的值,以便我可以在不同的测试中使用。尝试了所有的可能性,但它没有工作。不确定我做错了什么!

var supertest = require('supertest'),
    api = supertest('www.xyz.com');

    var my_token = 'DID NOT WORK';

    describe('get collars list', function(done) {   

        before(function(done) {
            api.post('/api/v2/auth')
                .send({username:"SP",password:"**"})
                .set('Content-Type', 'application/json')
                .expect(200)
                .end(function (err, res) {                  

                my_token = "worked"

                done();
            });
            console.log ('passing value to the test :  '+ my_token );
        });  

     it('should login', function(done) {     
       console.log (' token passed to test  : ' + my_token);
     });
    });
mocha supertest
1个回答
0
投票

在不涉及异步操作的测试用例中,您不需要done

这应该工作。

var supertest = require("supertest"),
  api = supertest("www.xyz.com");

var my_token = "DID NOT WORK";

describe("get collars list", function(done) {
  before(function(done) {
    api
      .post("/api/v2/auth")
      .send({ username: "SP", password: "**" })
      .set("Content-Type", "application/json")
      .expect(200)
      .end(function(err, res) {
        my_token = "worked";

        done();
      });
    console.log("passing value to the test :  " + my_token);
  });

  it("should login", function() {
    console.log(" token passed to test  : " + my_token);
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.