我正在尝试使用 TDD 方法运行我的测试,如下所示。我在另一个应用程序上运行相同的测试(我复制粘贴它)并且它有效,但在此测试中我收到以下错误:
TypeError: Attempted to wrap undefined property find as function Model file
/* 任务模型 数据库模型 */
const mongoose = require('mongoose');
try {
module.exports = mongoose.model('Task');
} catch (error) {
const taskSchema = mongoose.Schema({
title: { type: String, required: true },
status: { type: Boolean, required: true }
});
module.exports = taskSchema;
}
/* 示例测试 */
'use strict';
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const sinon = require('sinon');
const expect = chai.expect;
const Task = require('../../models/task');
chai.should();
chai.use(chaiAsPromised);
//TEST DRIVEN DEVELOPMENT
describe('Todo Controller', () => {
const expectedResult = { status: 201, tasks: [{}], message: '' };
//Testing if the array has a valid status
it('should get a valid status', done => {
const TodoMock = sinon.mock(Task);
TodoMock.expects('find').yields(null, expectedResult);
Task.find((err, result) => {
TodoMock.verify();
TodoMock.restore();
expect(result.status).to.be.equal(201);
done();
});
});
});
最佳答案
您似乎遇到了一些范围界定问题。我会这样编写模型:
const mongoose = require('mongoose');
const taskSchema;
try {
taskSchema = mongoose.model('Task');
} catch (error) {
taskSchema = mongoose.Schema({
title: { type: String, required: true },
status: { type: Boolean, required: true }
});
}
module.exports = taskSchema;
关于javascript - 类型错误 : Attempted to wrap undefined property find as function - NodeJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138555/