开发测试
单元测试
- 测试风格
- TDD 测试驱动开发
- BDD 行为驱动开发 目前公司使用的
- 测试框架
- mocha
- jest
- 断言
- power-assert
- assert
性能测试
- 基准测试
- benchmark.js
- 压力测试
- ab 目前使用的是ab
- siege
- http_load
使用jest完成测试
npm i jest -D 安装jest包
在package.json文件中添加
"scripts": {
"test": "jest --coverage",// --coverage可以测试代码覆盖率
},
"jest": {
"collectCoverage": true
}
具体配置: Configuring Jest 和 Jest CLI Options
在根目录下添加 test/demo.test.js并写入
'use strict';
describe('test demo', () => {
it('should be demo', () => {
// do somethings
});
运行
npm run test
MongoDB增删改查
增
insert() // 返回值为
WriteResult({ "nInserted" : 1 })
或
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
insertOne() // 返回值为
{
"acknowledged" : true,
"insertedId" : ObjectId("5b9793ac05c674531bf5bafc")
}
insertMany() // 返回值为
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5b9793e0b9b1bf5475b12ac5"),
ObjectId("5b9793e0b9b1bf5475b12ac6")
]
}
- insertMany 和 insertOne区别:insertMany能插入多个而insertOne只能插入一个
- insertMany 和 insert的区别:insert能插入一个或多个,且和insertMany返回值不同
删
deleteOne() //返回值为
{
"acknowledged" : true,
"deletedCount" : 1
}
deleteMany() //返回值为
{
"acknowledged" : true,
"deletedCount" : 2
}
改
updateOne(query, update, options) // 返回值为
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
updateMany(query, update, options) // 返回值为
{
"acknowledged" : true,
"matchedCount" : 2,
"modifiedCount" : 2
}
replaceOne(query, update, options) //返回值为
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
update(query, update, options)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
- update 和 updateMany区别:update可以更新一个或多个,需要使用 options里添加{multi:true}
- update 和 replaceOne区别:update不适用操作符的时,则和replaceOne一样为替换。使用操作符时,如$set,则为更新
查
find() // 数据list
findOne() // 数据list
在mongo shell中,当把find值赋给变量时,他不会自动迭代
var c = db.collection.find();
// 此时需要手动迭代游标 使用next()、forEach()或使用toArray()转换为数组来操作