【发布时间】:2015-04-30 16:59:45
【问题描述】:
我目前正在用 mocha 为我的 nodejs 应用程序编写测试。我的 api 调用要求我登录,所以我想创建一个包装测试套件,它创建一个测试用户,然后调用实际的测试套件。下面是代码的样子:
var request = require('supertest');
var config = require('../config/config');
var AdminUser = require('../models/Authmodel');
function configureAuth(test_suite) {
var url = "localhost:" + config.port;
var email = "test@test.com";
var password = "test_password";
var admin;
var token;
describe("Signup User", function() {
it("should signup new user", function(done) {
request(url)
.post('/auth/signup')
.send({
email: email,
password: password
})
.expect(200)
.end(function(){
done();
});
});
it("should login the user", function(done) {
request(url)
.post('/auth/login')
.send({
email: email,
password: password
})
.expect(200)
.end(function(err,res){
if(err)
throw(err);
res.body.should.have.property('token');
token = res.body.token;
done();
});
});
it("should retrieve admin document", function(done) {
AdminUser.findOne({email: email}, function(err, dbAdmin) {
if(err)
throw(err);
admin = dbAdmin;
done();
});
});
});
// Call the actual test suite, pass it the auth credentials.
describe("Test Suite", function() {
it("should run the test suite", function(done) {
// No matter what the timeout is set to it still exceeds it
this.timeout(5000);
test_suite({
email: email,
password: password,
token: token,
admin: admin
}, done);
});
});
describe("Clear Admins", function() {
it("should clear the admin table", function(done) {
AdminUser.remove({email: email}, function(err) {
if(err)
throw(err);
done();
});
});
});
};
module.exports = configureAuth;
这是一个使用包装器的测试套件:
var request = require('supertest');
var config = require('../config/config');
// Wrapper that creates admin user to allow api calls
var ConfigureAuth = require('./ConfigureAuth');
// Test data
var templateForm = {...}
var submittedForm = {...}
ConfigureAuth(
function(credentials, exit) {
var url = "localhost:" + config.port;
var templateFormId = null;
describe("Form Templates", function() {
describe('POST /api/form/template', function(){
it('should save the template', function(done){
request(url)
.post('/api/form/template')
.query({email: credentials.email, token: credentials.token})
.send({
_admin_id: credentials.admin._id,
template: templateForm,
})
.end(function(err, res){
templateFormId = res.body._id;
res.body.should.have.property('_admin_id').and.be.equal(''+credentials.admin._id);
res.body.should.have.property('template').and.be.instanceof(Object);
done();
});
});
});
describe('GET /api/form/template/:id', function(){
it('Should respond with template data', function(done){
request(url)
.get('/api/form/template/' + templateFormId)
.query({email: credentials.email, token: credentials.token})
.end(function(err, res){
...
done();
});
});
});
describe('GET /api/form/template/company/:id', function(){
it('Should respond with company template data', function(done){
request(url)
.get('/api/form/template/company/' + credentials.admin._id)
.query({email: credentials.email, token: credentials.token})
.end(function(err, res){
...
done();
});
});
});
describe('DELETE /api/form/template/:template_id', function(){
it('Should delete the template data', function(done){
request(url)
.delete('/api/form/template/' + templateFormId)
.query({email: credentials.email, token: credentials.token})
.end(function(err, res){
...
done();
});
});
});
});
describe("Submitted Forms", function() {
describe('POST /api/form/patient', function(){
it('should save submitted form', function(done){
request(url)
.post('/api/form/patient')
.query({email: credentials.email, token: credentials.token})
.send({
_admin_id: credentials.admin._id,
form: submittedForm,
firstName: "Jimbo",
lastName: "Cruise",
patientEmail: "jcruise@tomcruise.com",
})
.end(function(err, res){
...
submittedFormId = res.body._id;
done();
});
});
});
describe('GET /api/form/:form_id', function(){
it('should respond with submitted form data', function(done){
request(url)
.get('/api/form/patient/' + submittedFormId)
.query({email: credentials.email, token: credentials.token})
.end(function(err, res){
res.body.should.have.property('_id');
...
done();
});
});
});
});
after(function() {
exit();
});
});
无论我给测试套件什么超时,它都会给出“错误:超过 5000 毫秒的超时”。除了“它应该运行测试套件”之外,所有测试都通过了。我还要注意,我还有其他不使用包装器的测试文件。首先调用上面的这个测试套件,创建管理员用户,测试套件超时,然后清除管理文档,然后继续进行其他测试。最后,它打印出围绕 ConfigureAdmin 函数包装的测试。
【问题讨论】:
标签: node.js asynchronous mongoose mocha.js supertest