jjzjj

mocha-mongoose

全部标签

javascript - 如何在 mocha 中使用 should() 的 OR 条件

我想使用两个值进行比较,这样如果其中一个为真,那么我的测试应该通过。使用下面的代码只是比较第一个条件并且测试失败。if(typeoflng!=='undefined'){data.lng.should.equal(lng)||data.cityLng.should.equal(lng);我应该怎么做? 最佳答案 试试这个:if(typeoflng!=='undefined'){lng.should.be.equalOneOf(data.lng,data.cityLng);参见documentation.

javascript - 将键/值对添加到返回的 Mongoose 对象

我有检索Mongoose对象的代码,然后使用stripeCustomerId(存储在文档中)检索Stripecustomer对象(通过nodejsstripe)。然后我想将条纹customer对象附加到我的Mongoose对象。exports.getPlatformByCId=(cId)=>{returnnewPromise((resolve,reject)=>{Platform.find({clientId:cId}).then(response=>{letuser=response[0];stripe.customers.retrieve(user.stripeCustomerId

javascript - 如何封装mocha `expect()`代码?

我正在尝试测试是否存在一些我希望在所有测试中都需要的api响应属性(status和data属性)。这是一个通用测试,它断言supertest中的所需属性expect()方法:it('shouldcreateawidget',done=>{letstatus=200;request(test_url).post('/api/widgets').set('Authorization',`Bearer${token}`).send({sku:my_widget_data.sku,name:my_widget_data.name,description:''}).expect(res=>{as

javascript - Mongoose 的循环引用

我有以下Mongoose模式代码varEstacionSchema=newSchema({nombre:{type:String,required:true,unique:true},zona:{type:String,required:true},rutas:[Ruta]})mongoose.model('Estacion',EstacionSchema)varRutaSchema=newSchema({nombre:{type:String,required:true,unique:true,uppercase:true},estaciones:[Estacion]})mongoo

javascript - mocha js 断言在使用 promise 时挂起?

"usestrict";letassert=require("assert");describe("Promisetest",function(){it('shouldpass',function(done){vara={};varb={};a.key=124;b.key=567;letp=newPromise(function(resolve,reject){setTimeout(function(){resolve();},100)});p.then(functionsuccess(){console.log("success---->",a,b);assert.deepEqual

javascript - 使用 Sinon/Mocha 正确 stub 请求/响应

我对后端单元测试比较陌生,需要一些关于如何对以下内容进行单元测试的指导。我正在使用Mocha/Should/Sinon。exports.get=function(req,res){if(req.query.example){returnres.status(200).json({success:true});}else{returnres.status(400).json({error:true});}} 最佳答案 您可以使用Sinon的spy和stub函数来测试您的代码,如下所示:const{spy,stub}=require('

javascript - 如何使用 mocha/chai/chai-as-promised 测试 ES7 异步函数

我有以下功能要测试://...constlocal=newWeakMap();exportdefaultclassUser{//...asyncpassword(password){if(!password)returnlocal.get(this).get('hash');//removethisforsecurityreasons!if(password.length现在我想用mocha测试这个函数,chai和chai-as-promised做这个测试用例:importchaifrom'chai';importchaiAsPromisedfrom'chai-as-promised'

javascript - 如何更新 mongoose 中查询前或查询后 Hook 的文档?

我正在尝试更新查询Hook上的字段。例如:varmySchema=newSchema({name:String,queryCount:{type:Number,default:0}});我想在每个find或findOne查询上增加和更新queryCount字段。mySchema.post('find',function(doc){//hereisthemagic});我尝试了一些事情,但到目前为止没有成功。我可以在模型中实现它还是必须在Controller中实现? 最佳答案 你想要的是一个postinithookmySchema.p

javascript - Mongoose 每次使用预保存钩子(Hook)保存时都会更改密码

我正在使用带有bcrypt的预保存Hook来加密系统上的密码。它在创建或更改密码时工作正常。问题是每次我更改并保存不同的字段(例如电子邮件)时,它似乎都会重新加密密码。可能更容易用代码解释。这是模型:constUserSchema=newSchema({email:{type:String,required:true,lowercase:true,unique:true,trim:true},password:{type:String,required:true}})还有钩子(Hook):UserSchema.pre('save',function(next){constuser=th

javascript - Mongoose - TypeError : object is not a function

我正在尝试将Mongoose模型从我的model/user.model.js文件导出到我的服务器目录中的server.js文件。模型/user.model.jsvarmongoose=require('mongoose');varSchema=mongoose.Schema();varUserSchema=newSchema({instagramId:{type:String,index:true},email:{type:String,unique:true,lowercase:true},password:{type:String,select:false},userName:St