【问题标题】:TypeError: Attempted to wrap undefined property find as function - NodeJsTypeError:尝试将未定义的属性查找包装为函数-NodeJs
【发布时间】:2019-09-27 16:43:49
【问题描述】:

我正在尝试使用 TDD 方法按如下方式运行我的测试。我在另一个应用程序上运行了相同的测试(我复制粘贴它)并且它可以工作,但在这个测试中我收到以下错误:

TypeError: 试图将未定义的属性查找包装为函数 模型文件

/* 任务模型 数据库模型 */

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();
    });
  });

});

【问题讨论】:

  • 提及您的“../../models/task”文件以获得更好的想法。
  • @Dipten 完成先生

标签: javascript node.js mocha.js sinon chai


【解决方案1】:

您似乎有一些范围界定问题。我会这样写模型:

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多