1

我正在尝试在 after 方法中删除在 mocha 单元测试期间创建的用户,但它不起作用,我通常是 mocha 和 nodejs 的新手,所以感谢您的洞察力

var request = require('superagent');
.
.
after(function(){
request.del(baseurl + 'api/users/' + id)
    .end(function(e, res){
        console.log(res.body);
    });
});

下面是整个测试文件

var request = require('superagent');
var expect  = require('expect.js');
var baseurl = 'http://localhost:5000/';

var name = makeid(7);
var email = makeid(7) + '@someemail.com';
var id;

function makeid(s)
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for( var i=0; i < s; i++ )
    text += possible.charAt(Math.floor(Math.random() * possible.length));

 return text;
}

before(function(){
console.log("before")
});
describe('Test', function(){
beforeEach(function(){
    console.log("..........")
});

it('Create User', function(done){
    request.post(baseurl + 'api/users')
        .send({     name: name
            , email: email
        })
        .end(function(e,res){
            console.log("res.text: " + res.text)
            console.log("res.status: " + res.status)
            id = res.body._id;
            console.log("Retrieved id is : " + id)
            done()
        })
   });
//THIS WORKS
it('Delete', function(done){
    console.log("deleting created user.")
    request.del(baseurl + 'api/users/' + id)
        .end(function(e, res){
            console.log(res.body);
            done()
        })
});
 });
  //THIS DOESNT
 after(function(){
request.del(baseurl + 'api/users/' + id)
    .end(function(e, res){
        console.log("NOTHING GETS PRINTED HERE");
    });

console.log("deleted")
 });
4

0 回答 0