Mongo模型
mongo模型
在MongoDB里面創(chuàng)建一個(gè)model(collection)
在src里面建立一個(gè)user.js文件
//define user_model
const mongoose = require('mongoose');
//allow us to create a schema for our user model
const Schema = mongoose.Schema;
//create a schema of user_model
const UserSchema = new Schema({
name: String
});
//創(chuàng)建一個(gè)'user'的collection羡棵,使用UserSchema這個(gè)schema
//User = entire collection
const User = mongoose.model('user', UserSchema);
//整個(gè)項(xiàng)目都能使用User的reference
module.exports = User;
向數(shù)據(jù)庫(kù)里插入記錄
在test里面建立create_test.js
//插入記錄
//引用斷言
const assert = require('assert');
//describe function
describe('Creating records', () => { //describe string and it function
it('saves a user', () => { //individual it statement
//assert(1 + 1 === 2); //添加斷言讓測(cè)試成功
assert(1 + 1 === 3); //添加斷言讓測(cè)試失敗
});
});
Mocha如何工作
Mocha
- Describe function 里面有多個(gè) IT function壹若。
- Describe function 和 IT function 的參數(shù)都有兩個(gè),都是String 和 function:
describe(String, it(String, assertion))
- assertion: 把變量作比較皂冰,然后Mocha給出解釋店展,告訴我們測(cè)試結(jié)果。
使用Mocha測(cè)試
- 在有package.json的目錄下使用命令
npm run test
- 打開(kāi)package.json
剛剛收到的錯(cuò)誤信息
- 輸入測(cè)試命令
"scripts": {
"test": "mocha"
},