草庐IT

Karma-mocha

全部标签

node.js - 如何在 Mocha 单元测试中使用 Mongoose ?

感觉很迷茫,怎么在mocha中单元测试涉及到mongodb,我还是不能成功调用save函数,没有异常抛出。我尝试用最简单的例子进行测试,发现还是有问题,这是我的代码。varassert=require("assert")varmongoose=require('mongoose');mongoose.connect('mongodb://localhost/dev',function(err){if(err)throwerr});describe('incrementId',function(){describe('increment',function(){it('shouldhas

node.js - 单元/集成测试 Express REST API、mongoose、mocha、sinon、chai、supertest

我对使用Mongoose对ExpressRESTAPI进行单元测试感到很困惑。我听说过supertest、sinon、chai和mocha。我想编写测试:1)测试API接口(interface):创建一个super测试服务器并向其发出请求(例如GET/users、POST/users)并检查它是否调用了正确的路由。2)测试Mongoose查询:当路由被调用时,我想检查是否调用了正确的mongoose函数(find,findById)并且它返回了正确的数据(假数据),比如:当调用User.findId时,返回假用户(在beforeEach中定义的数据)。这是sinonstub的用途吗?3

node.js - Mocha 路由测试不异步执行

我已经开始使用mocha,但我遇到了一个特定测试用例的问题。这是代码:varassert=require("chai").assert;varrequest=require('supertest');varhttp=require("http");varconf=require("../config/config");varapp=require("../app");varmongoose=require('mongoose');varUser=mongoose.model('User');describe('Usercontroller',function(){describe('P

node.js - 如果一次运行超过 4 个测试,Mocha 测试超时

我有一个node.js+express网络服务器,我正在使用Mocha进行测试。我在测试工具中启动Web服务器,并连接到mongodb以查找输出:describe("Api",function(){before(function(done){//startserverusingfunctionexportedfromanotherjsfile//connecttomongodb});after(function(done){//shutdownserver//closemongoconnection});beforeEach(function(done){//emptymongocol

node.js - 为什么 Mocha 断言在 mongoose.Schema.objectId 上失败?

我在mocha的某个地方声明为:assert.equal(model.organizationId,objId);但我得到失败的mocha结果:UncaughtAssertionError:"5225777180a843d901000012"=="5225777180a843d901000012"为什么会发生,当ID相同时。如何克服它? 最佳答案 所以在这方面,javascript中的mongodbObjectIds令人沮丧。它们是不同的对象,导致obj1===obj2评估为false,即使它们表示的值相同。有3个选项:在比较之前转

node.js - 如何在 mocha 和 mongoDB 中使用 chai 向 Node 发出发布请求

如何在mocha和mongoDB中使用chai向Node发出POST请求。我有一个包含我的获取请求和发布请求的测试文件。我下面的代码等于我的get请求,它通过了我为其设置的1次测试,但是我在创建我的发布请求时遇到了问题,我不明白我应该做什么。获取请求:constchai=require('chai');constexpect=chai.expect;constchaiHttp=require('chai-http')chai.use(chaiHttp)describe('site',()=>{//Describewhatyouaretestingit('Shouldhavehomepa

javascript - 使用 Mocha 和 Sequelize 测试模型

我想在mysql数据库上测试CRUD操作,使用Sequelize作为ORM和Mocha/Chai进行单元测试。我使用http路由测试了记录的插入/删除,但我会直接测试模型而不使用任何http连接。我尝试这样做,但是当我启动测试时,没有添加记录并且我没有收到任何错误。app/model/article.jsmodule.exports=function(sequelize,DataTypes){varArticle=sequelize.define('Article',{title:DataTypes.STRING,url:DataTypes.STRING,text:DataTypes.

mysql - 无法以同步模式运行 Mocha.js

我正在使用在nodejs实例中运行的mocha测试存储过程。我有这个测试骨架:varchai=require('chai'),MyReporter=require("../MyReporter.js"),chokidar=require('chokidar'),expect=chai.expect,should=chai.should,assert=chai.assert;varMocha=require('mocha');amochai=newMocha({bail:false,debug:true});amochai.addFile("mytest_v1.js");function

node.js - 使用 mocha 测试 Node tcp 服务器

我有一个tcp服务器,我想用mocha测试它://Starttheserverrequire('net').createServer(function(socket){//Handleincomingdatasocket.on('data',function(data){...somestuffsocket.write("replywithsomeerrmessageifany");});});我一般用expressjs开发node应用,用于HTTPRestAPI,使用grunt-express-server模块,比如:grunt.registerTask('validate',['e

javascript - 运行 mocha 测试但出现错误 : ReferenceError: document is not defined

我正在使用mocha测试框架来测试Httprest-api。我想为所有测试用例生成测试报告,但是当我运行mocha--reporterhtml>report.html出现跟随错误/usr/local/lib/node_modules/mocha/lib/reporters/html.js:263vardiv=document.createElement('div');^ReferenceError:documentisnotdefinedatfragment(/usr/local/lib/node_modules/mocha/lib/reporters/html.js:263:13)a