使用ts-mockito模拟猫鼬模式时出错:“ find()不是函数”

时间:2019-01-29 04:41:55

标签: typescript

我正在使用ts-node运行Typescript。我正在尝试与摩卡咖啡和柴进行测试。我还开玩笑地测试了我的测试。基本上,大多数测试运行良好。但是,当我尝试模拟我的猫鼬模式模型对象时,我得到了错误mockThread.find不是函数。奇怪的是,当我将鼠标悬停在模拟对象上或悬停在模拟对象上时,我可以看到所有的猫鼬查询方法,包括find。仅当测试运行时才会发生此故障。如果我删除了模拟程序并仅运行测试,它将成功。

import { mock, when, instance } from "ts-mockito";
import { expect } from "chai";
import request from "supertest";
import app from "../../src/app";
import { isJSONString } from "../testHelper";
import { ThreadSchema } from "../../src/repo/models/thread";
import mongoose, { Model, DocumentQuery, Document } from "mongoose";

const Thread = mongoose.model("Thread", ThreadSchema);

describe("Test whether result is array and json stringifyable.", () => {
it("Should return the error", done => {
let mockThread: Model<Document, {}> = mock<Model<Document, {}>>(Thread);
// I've tried adding and removing the typing info above but that did nothing
when(mockThread.find()).thenReturn(
  new DocumentQuery<Document[], Document, {}>()
);

request(app)
  .get("/threads")
  .expect(200)
  .end(function(err, res) {
    expect(isJSONString(res.body)).to.equal(true);
    expect(Array.isArray(res.body)).to.equal(true);
    done();
  });
});
});

我使用此命令mocha运行测试--require ts-node / register test / ** / *。ts

0 个答案:

没有答案