【发布时间】:2019-11-18 22:11:24
【问题描述】:
背景故事
我正在玩一个名为screeps 的游戏,该游戏允许您编写一个在游戏中执行动作的机器人/人工智能。实现这一点的方法是上传在具有可用常量、类和定义的环境中运行的代码。
这些定义是用以下包定义的 https://www.npmjs.com/package/@types/screeps
现在我在 typescript 中编写代码并使用汇总来编译它以用于 screeps,我正在使用以下入门包 https://github.com/screepers/screeps-typescript-starter
我正在尝试使用以下框架进行模拟 https://www.npmjs.com/package/@fluffy-spoon/substitute
问题
在 mocha 中运行时,我必须从游戏中存根/模拟环境的功能,在测试框架中运行时它不存在。
我设法模拟了常量,并在其他人的帮助下让它们在 mocha 中运行。但现在我坚持扩展对象 Creep
原型.ts
Object.defineProperty(Creep.prototype, "task", {
get() {
... logic
},
set(task: ITask | null) {
... logic
}
})
测试
import "../constants"
import "../../src/tasks/prototypes"
import { Task } from "tasks/Task"
import { Game, Memory } from "./mock"
import { assert } from "chai"
import { Substitute, Arg } from "@fluffy-spoon/substitute"
describe("tasks", () => {
before(() => {
// runs before all test in this block
})
beforeEach(() => {
// runs before each test in this block
// @ts-ignore : allow adding Game to global
global.Game = _.clone(Game) as Game
// @ts-ignore : allow adding Memory to global
global.Memory = _.clone(Memory)
})
it("Creep should have extended prototyped", () => {
const testCreep = Substitute.for<Creep>()
const task = testCreep.task
assert.isNotNull(task)
})
})
当我运行测试时,我收到以下错误
...\dist\test-unit.bundle.js:3324
}(柴));
^
ReferenceError:未定义蠕变
这是有道理的,因为游戏引擎或测试环境没有定义cree类,所以导入prototypes.ts无法扩展它 但我不确定如何真正让它发挥作用。
【问题讨论】:
标签: typescript unit-testing mocking mocha.js screeps