无法将参数从 api 请求传递到外部
Unable to pass parameter from the api request to outside
下面的代码是一个简单的 mocha 测试,我试图传递变量 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);
});
});
不涉及异步操作的测试用例不需要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);
});
});
下面的代码是一个简单的 mocha 测试,我试图传递变量 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);
});
});
不涉及异步操作的测试用例不需要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);
});
});