我是单元测试概念的新手,我必须测试为我的"创建"将记录存储到数据库表(mySQL)的函数。这是我的控制器代码:
'use strict'
var todo = require('../../../repository/todo-repo')
module.exports = {
collect: (req,res,next)=>{
var fields = ['id','title','description'];
var reqdata = new req.collect(req,fields);
req.task_data = reqdata.collect(req,res)
if(!req.task_data.length) return (new Error("no values provided"))
console.log(req.task_data);
next();
},
create: (req,res,next) =>{
var options = {
data:req.task_data
}
todo.create(options,success,error)
function success(result){
console.log(result);
}
function error(err){
if(err) return (err)
}
}
}
这是我的测试代码:
var chai = require('chai')
var expect = chai.expect;
var todo = require('../controllers/api/todo/todo')
var todomodal = require('../models/todo')
var create = todo.create;
var sinon = require('sinon');
describe('create',()=>{
it('should add data to the todo table',(done)=>{
var req = {
task_data:{
title:"here",
description:"details to follow"
}
}
var id = create(req,null,null)
console.log("=>>",id);
expect(id).to.be.a('string')
done()
})
})
如何编写测试以期望我的函数获得某个结果。让我们说我想将测试数据发送给函数,并期望创建的记录的id是一个整数。由于sequelize返回成功和错误两个回调,我该如何处理?