草庐IT

mocha-mongoose

全部标签

javascript - Mocha 和 this 上下文

所以我有这段代码:describe('maindescribe',function(){afterEach(function(){//this.prop===undefined});describe('sub',function(){it('shoulddosomething',function(){this.prop='test';});});});我不知道为什么mainafterEach中的this.prop是undefined因为下面的代码按预期工作:describe('maindescribe',function(){afterEach(function(){//this.pr

javascript - 如何计算 Mongoose 中具有一个不同字段的记录?

在为nodejs探索mongoose时,我遇到了需要知道我的集合中的用户数量的问题:我的收藏有记录,每条记录都有一个用户。我想知道唯一(不同)用户的数量。如何使用mongoose执行此操作?编辑:数据库增长得非常快,有没有办法从数据库中取回数字而不是获取所有不同的记录并计算它们? 最佳答案 这是一个替代答案,因为当我尝试使用Reddest的Mongoose3.1.2方法时出现异常(对我来说这似乎是Mongoose中的一个错误,因为Reddest的方法应该没问题)。您可以在集合的模型上调用distinct方法,指定该集合的用户标识字段

javascript - Jasmine spyOn mongoose 保存

我想模拟Mongoose模型的save()函数。我要测试的函数在名为user.js的文件中如下所示:varUser=import('User.js')post:function(req,res){varuser=newUser({password:req.body.password,email:req.body.email,});user.save(function(err){if(err){....}else{....}});我试图在另一个名为user_spec.js的文件中编写如下所示的测试:varHander=require('user.js')it('worksproperly

javascript - 是否可以在 Mocha 测试中使用 ES6 模块?

ES6、Windows10x64、Node.js8.6.0、Mocha3.5.3是否可以在Mocha测试中使用ES6模块?我遇到了export和import关键字的问题。/*eventEmitter.js*//*Eventemitter.*/exportdefaultclassEventEmitter{constructor(){constsubscriptions=newMap();Object.defineProperty(this,'subscriptions',{enumerable:false,configurable:false,get:function(){returns

javascript - Cloud9 中的 Mocha 测试出现 "undeclared variable"警告

我是node.js和用于单元测试的框架Mocha的新手,但我在cloud9IDE中创建了几个测试只是为了看看它是如何工作的。代码如下所示:varassert=require("assert");require("should");describe('Array',function(){describe('#indexOf()',function(){it('shouldreturn-1whenthevalueisnotpresent',function(){assert.equal(-1,[1,2,3].indexOf(5));assert.equal(-1,[1,2,3].index

javascript - 在 Mocha 浏览器中使用多个记者?

是否可以在浏览器版本的Mocha中使用多个记者?我正在创建一个将测试结果发送到我的服务器的记者,但我仍然想使用Mocha默认的默认HTML记者。现在我正在修改源代码以使其正常工作。我知道Mocha也为其记者使用commonJS。 最佳答案 mocha.run返回一个运行器对象,它发出有用的事件,例如'testend'和'suiteend'。因此,我们可以这样做:mocha.run().on('testend',function(test){if('passed'===test.state){console.log('passed!'

javascript - 如何从测试文件中访问 Mocha 选项?

我正在使用gruntjs和grunt-simple-mocha运行mocha测试:https://github.com/yaymukund/grunt-simple-mocha如何在每个mocha测试中访问grunt.js文件中定义的选项?我想要完成的是在我的gruntfile中有一些通用配置,并在我的测试中使用它。 最佳答案 我已经找到的一种方法是使用全局值,这不是很好,但是有效在grunt.js配置中global.hell='heyyou';内部测试console.log(global.hell);现在多检查一种方式,也许会更好

javascript - 如何在 "before" Hook 中获取 Mocha 测试名称?

我正在尝试在beforeHook中获取当前的describe名称,如下所示:describe('increasing3times',function(){before(function(){console.log('testname');});...});我基本上想在before钩子(Hook)中检索'increasing3times'字符串。如何实现?谢谢! 最佳答案 下面是说明如何执行此操作的代码:describe("top",function(){before(function(){console.log("fulltitle

javascript - 环回和 mocha : wait for server to finish boot scripts

您好,作为问题的标题,我想知道如何在启动测试之前检查环回引导脚本是否已完成。在示例项目中:https://github.com/strongloop/loopback-example-relations有一个file在似乎可以完成工作的测试文件夹中,但不幸的是它没有解决它。start-server.js:varapp=require('../server/server');module.exports=function(done){if(app.loaded){app.once('started',done);app.start();}else{app.once('loaded',fu

javascript - 使用 Mongoose 在 JSLint 中抑制 `Expected an identifier and instead saw ' default'(保留字)

我正在使用jshint来验证我的JavaScript文件。在服务器端,我将node.js与Mongoose结合使用。在Mongoose中,我被鼓励以如下方式编写模式:varUserSchema=newmongoose.Schema({firstname:{type:String,default:''}});运行linting时,出现错误:Expectedanidentifierandinsteadsaw'default'(areservedword).有没有办法抑制这个错误?我真的更喜欢这种行为而不是写作:varUserSchema=newmongoose.Schema({firstn