我有以下架构。
//models/line.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Line = new Schema({
key: {type: String, unique: true},
name: {type: String, unique: true},
color: [Number],
linePaths: [{type: Schema.Types.ObjectId, ref: 'LinePath'}]
});
module.exports = mongoose.model('Line', Line);
我目前正在尝试使用以下代码对其进行单元测试:
const mockingoose = require('mockingoose').default;
const lineModel = require('../../models/line');
describe('test mongoose line model', () => {
it('should return the line with findById', () => {
const lineData = { _id: '5fac7d07d0d2182b885555ee', key: 'test_key', name: 'lineNameTest', color: [], linePaths: [] };
const createdLine = new lineModel(lineData);
mockingoose(lineModel).toReturn(lineData, 'findOne');
return lineModel.findById({ _id: '5fac7d07d0d2182b885555ee' }).then(line => {
expect(line).not.toBeNull();
});
});
});
我已经尝试使用expect(line).not.toBeNull()
而不是expect(line).toMatchObject(createdLine)
并得到以下错误:
expect(received).toMatchObject(expected)
- Expected - 2
+ Received + 80
Object {
+ "$__": Object {
+ "$options": Object {
+ "defaults": true,
+ },
+ "$setCalled": Set {
+ "_id",
+ "key",
+ "name",
+ "color",
+ "linePaths",
+ },
"_id": "5fac7d07d0d2182b885555ee",
- "color": Array [],
+ "activePaths": Object {
+ "forEach": [Function anonymous],
+ "paths": Object {
+ "_id": "modify",
+ "color": "modify",
+ "key": "modify",
+ "linePaths": "modify",
+ "name": "modify",
+ },
+ "stateNames": Array [
+ "require",
+ "modify",
+ "init",
+ "default",
+ "ignore",
+ ],
+ "states": Object {
+ "default": Object {},
+ "ignore": Object {},
+ "init": Object {},
+ "modify": Object {
+ "_id": true,
+ "color": true,
+ "key": true,
+ "linePaths": true,
+ "name": true,
+ },
+ "require": Object {},
+ },
+ },
+ "adhocPaths": undefined,
+ "cachedRequired": Object {},
+ "emitter": EventEmitter {
+ "_events": Object {},
+ "_eventsCount": 0,
+ "_maxListeners": 0,
+ Symbol(kCapture): false,
+ },
+ "fullPath": undefined,
+ "getters": Object {},
+ "inserting": undefined,
+ "ownerDocument": undefined,
+ "pathsToScopes": Object {
+ "color": [Circular],
+ "linePaths": [Circular],
+ },
+ "populate": undefined,
+ "populated": undefined,
+ "removing": undefined,
+ "saveError": undefined,
+ "saving": undefined,
+ "scope": undefined,
+ "selected": undefined,
+ "session": null,
+ "shardval": undefined,
+ "strictMode": true,
+ "validationError": undefined,
+ "version": undefined,
+ "wasPopulated": false,
+ },
+ "$locals": Object {},
+ "$op": null,
+ "_doc": Object {
+ "_id": "5fac7d07d0d2182b885555ee",
+ "color": CoreMongooseArray [],
"key": "test_key",
- "linePaths": Array [],
+ "linePaths": CoreMongooseArray [],
"name": "lineNameTest",
+ },
+ "errors": undefined,
+ "isNew": true,
}
但是我对期望所做的任何更改我似乎都无法获得关于此类的任何报道(或与此相关的任何其他报道)。 谁能告诉我我做错了吗?
答案 0 :(得分:0)
根据建议,我将代码更改为以下内容:
return lineModel.findById({ _id: '5fac7d07d0d2182b885555ee' }).lean().then(line => {
expect(JSON.parse(line)).toEqual(JSON.parse(lineData ));
});
并且没有任何错误地通过了测试。