gpt4 book ai didi

node.js - 使用 Mocha 进行 NodeJS 测试。

转载 作者:太空宇宙 更新时间:2023-11-04 00:16:06 24 4
gpt4 key购买 nike

我目前正在尝试使用 Mocha 和 Chai 测试应用程序,但在将其中一个模块连接到测试时遇到困难。

这是我的测试用例:

"use strict";

var chai = require('chai');
var expect = require("chai").expect;
var helloWorld = require("../routes");
var mongoose = require("mongoose");
var app = require("../app");
var application = require("../routes");

describe('helloWorld', function () {
it('Mongo Module extended', function () {
expect(helloWorld()).equal('Mongo Module Extended');
});
});

describe('application', function application(app){
it('connects properly', function(done) {
expect(application(app))
.request('http://localhost:80')
.get('/')
.end(function(err, res) {
expect(res).to.have.status(200);
done(); // <= Call done to signal callback end
});
});
});

这是我当前正在尝试测试的文件:

var passport = require('passport');
var Account = require('./models/account');
var path = require('path');
var mongojs = require('mongojs');
var dbx = mongojs('test', ['logs']);
var fs = require('fs');
var dbc = mongojs('test', ['accounts']);


function helloWorld() {
return 'Mongo Module Extended';
}

module.exports = helloWorld;


function application(app) {

app.get('/',
function(req, res){
res.sendFile('login.html', {root: __dirname});
});

app.get('/login',
function(req, res){
res.sendFile(path.join(__dirname + '/login.html'));
});

app.get('/index', isLoggedIn,
function(req, res){
req.flash('info', 'Flash is back!')
res.sendFile(path.join(__dirname + '/views/index.html'));
});
app.get('/', isLoggedIn,
function(req, res){
res.sendFile(path.join(__dirname + '/login.html'));
});

app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err);
}
if (!user) {
return res.json({success:false, message: "Wrong Username or Password"}); //sends json message to the Front end jQuery function
}
req.logIn(user, function(err) {
if (err) {
return next(err);
}
res.json({success:true, redirectTo: '/index', message: "You are logged-in."}); //sends json message to the Front end jQuery function
/* Add username and time of login to the collection in MongoDB */
dbc.accounts.findOne(function(err, info){
var users = info.username; //Gets the logging username from the collection
var date = new Date().toLocaleDateString(); // generates a new date.
console.log(date);

console.log(date +" "+ "date");
var stamp = {name: users, time:date}; // object to hold both variables.
//toLocaleDateString('en-GB')
dbx.logs.insert(stamp, function(err, result){ //query to insert one inside the "logs" collection.
if(err) { throw err; }else{console.log("added" + JSON.stringify(stamp));}
});
});
/* END of Collection Logging Method */
});
})(req, res, next);
});


app.get('/logout',
function(req, res){
req.logout();
res.redirect('/login');
});


function isLoggedIn(req, res, next) {
//console.log('here is Authenticated', req.isAuthenticated()) //prints out 'here is Authenticated' if the Passport login is successful
if (req.isAuthenticated()){
console.log('here is Authenticated');
return next();
}else{
console.log("you cannot access the routes without being logged in!");
}

}

module.exports = application;

我不断收到此错误:

TypeError: expect(...).request is not a function

我猜这是引用我尝试在应用程序文件中发出的第一个获取请求:

app.get('/',
function(req, res){
res.sendFile('login.html', {root: __dirname});
});

此时我不太确定如何解决这个问题。我知道我的错误在于我尝试测试 get 请求,但我似乎无法绕过它。

如何更正我的代码,以便正确引用 module.exports = application; 中的 GETPOST 方法?

最佳答案

Chai 本身不支持 http 路由测试,您需要 chai-http 来做到这一点。
您可以查看更多链接:https://scotch.io/tutorials/test-a-node-restful-api-with-mocha-and-chai

关于node.js - 使用 Mocha 进行 NodeJS 测试。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com