Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用node.js和azure函数与SQL进行单元测试的应用程序_Javascript_Sql_Node.js_Azure_Unit Testing - Fatal编程技术网

Javascript 使用node.js和azure函数与SQL进行单元测试的应用程序

Javascript 使用node.js和azure函数与SQL进行单元测试的应用程序,javascript,sql,node.js,azure,unit-testing,Javascript,Sql,Node.js,Azure,Unit Testing,如何开始单元测试我的应用程序? 首先,我想测试我是否可以访问我的SQL数据库。 其次,我想测试如何测试新用户的创建 我似乎找不到关于这个问题的任何文件 我可以使用哪种测试框架,mocha&chai 这是我的azure注册函数 module.exports = async function (context, req) { context.log('JavaScript HTTP trigger function processed a request.') try {

如何开始单元测试我的应用程序? 首先,我想测试我是否可以访问我的SQL数据库。 其次,我想测试如何测试新用户的创建

我似乎找不到关于这个问题的任何文件

我可以使用哪种测试框架,mocha&chai

这是我的azure注册函数

    module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.')
    try {
        await db.startDb(); //start db connection
    } catch (error) {
        console.log("Error connecting to the database", error.message)
    }
    
    switch (req.method) {
        case 'GET':
            await get(context, req);
            break;
        case 'POST':
            await post(context, req);
            break
        default:
            context.res = {
                body: "Please get or post"
            };
            break
    }
}

async function get(context, req){
    try{
        let name = req.query.name;
        let user = await db.select(name)
        context.res = {
            body: user
        };
    } catch(error){
        context.res = {
            status: 400,
            body: `No user - ${error.message}`
        }
    }
}

async function post(context, req){
    try{
        let payload = req.body;
        await db.insert(payload)
        console.log(payload)
        
        context.res = {
            body: ["succes"]
        }
    } catch(error){
        context.res = {
            status: 400,
            body: error.message
        }
    }
}
这是我的数据库文件

const { Connection, Request, TYPES } = require('tedious');
const config = require('./config.json');
const jwt = require("jsonwebtoken");
const safeJWT = require("../middleware/Jwt")
const bcrypt = require("bcryptjs");

var connection = new Connection(config);

function startDb() {
    return new Promise((resolve, reject) => {
        connection.on('connect', (err) => {
            if (err) {
                console.log('Connection failed')
                reject(err)
                throw (err);
            } else {
                console.log('Connected')
                resolve();
            }
        })
        connection.connect();
    })
}
module.exports.sqlConnection = connection
module.exports.startDb = startDb
    fetch("http://localhost:7071/api/register", {
        method: 'POST',
        body: JSON.stringify({
            username: username,
            email: email,
            password: password
        }),
        headers: {
            "Content-Type": "application/json; charset-UTF-8"
        }
    }).then((response) =>
        response.json()).then((data) => {
            if(data[0] = "succes"){
            location.href = "login.html"
            } else {
                alert("failed")
            }
        }).catch((err) => {
            console.log(err)
        })
})
这是我的register.js文件的一部分

const { Connection, Request, TYPES } = require('tedious');
const config = require('./config.json');
const jwt = require("jsonwebtoken");
const safeJWT = require("../middleware/Jwt")
const bcrypt = require("bcryptjs");

var connection = new Connection(config);

function startDb() {
    return new Promise((resolve, reject) => {
        connection.on('connect', (err) => {
            if (err) {
                console.log('Connection failed')
                reject(err)
                throw (err);
            } else {
                console.log('Connected')
                resolve();
            }
        })
        connection.connect();
    })
}
module.exports.sqlConnection = connection
module.exports.startDb = startDb
    fetch("http://localhost:7071/api/register", {
        method: 'POST',
        body: JSON.stringify({
            username: username,
            email: email,
            password: password
        }),
        headers: {
            "Content-Type": "application/json; charset-UTF-8"
        }
    }).then((response) =>
        response.json()).then((data) => {
            if(data[0] = "succes"){
            location.href = "login.html"
            } else {
                alert("failed")
            }
        }).catch((err) => {
            console.log(err)
        })
})

油井单元测试是单独进行的,他们测试的是一个单元。若要使用将进行e2e测试的数据库测试用户创建,则不应测试数据库等

对于单元测试,模拟数据库调用