[Unit Testing] Test Mongoose model
作者:互联网
Model:
import mongoose from 'mongoose'
const itemSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
maxlength: 50
},
status: {
type: String,
required: true,
enum: ['active', 'complete', 'pastdue'],
default: 'active'
},
notes: String,
due: Date,
createdBy: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'user',
required: true
},
list: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'list',
required: true
}
},
{ timestamps: true }
)
// 1: means sorting order
// order matters, list first, then name
itemSchema.index({ list: 1, name: 1 }, { unique: true })
export const Item = mongoose.model('item', itemSchema)
Test:
import { Item } from '../item.model'
import mongoose from 'mongoose'
describe('Item model', () => {
describe('schema', () => {
test('name', () => {
const name = Item.schema.obj.name
expect(name).toEqual({
type: String,
required: true,
trim: true,
maxlength: 50
})
})
test('status', () => {
const status = Item.schema.obj.status
expect(status).toEqual({
type: String,
required: true,
enum: ['active', 'complete', 'pastdue'],
default: 'active'
})
})
test('notes', () => {
const notes = Item.schema.obj.notes
expect(notes).toEqual(String)
})
test('due', () => {
const due = Item.schema.obj.due
expect(due).toEqual(Date)
})
test('createdBy', () => {
const createdBy = Item.schema.obj.createdBy
expect(createdBy).toEqual({
type: mongoose.SchemaTypes.ObjectId,
ref: 'user',
required: true
})
})
test('list', () => {
const list = Item.schema.obj.list
expect(list).toEqual({
type: mongoose.SchemaTypes.ObjectId,
ref: 'list',
required: true
})
})
})
})
标签:const,type,Testing,list,Item,mongoose,Test,model,true 来源: https://www.cnblogs.com/Answer1215/p/16617493.html