【问题标题】:How do you mock & extend an external defined class and it's prototypes?您如何模拟和扩展外部定义的类及其原型?
【发布时间】: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


    【解决方案1】:

    我设法通过按照我在常量定义中所做的事情来让它运行

    模拟.ts

    import { Substitute, Arg } from "@fluffy-spoon/substitute"
    
    export const Game = Substitute.for<Game>()
    
    const mockScreeps = () => {
      const g = global as any
    
      g.Game = Game
      g.Creep = (function() {
        function Creep() {}
    
        return Creep
      })()
    
      g.RoomObject = (function() {
        function RoomObject() {}
    
        return RoomObject
      })()
    
      g.RoomPosition = (function() {
        function RoomObject() {}
    
        return RoomObject
      })()
    }
    
    mockScreeps()
    

    测试

    import "../constants"
    import { Memory } from "./mock"
    import { assert } from "chai"
    import "../../src/tasks/prototypes"
    
    describe("tasks", () => {
      before(() => {
        // runs before all test in this block
      })
    
      beforeEach(() => {
        // runs before each test in this block
        // @ts-ignore : allow adding Memory to global
        global.Memory = _.clone(Memory)
      })
    
      it("Creep should have extended prototyped", () => {
        const creep = new Creep("test")
        creep.memory = {
          task: {
            tick: 123,
            name: "dummy"
          }
        }
    
        const task = creep.task
        assert.isNotNull(task)
        if (task && creep.memory.task) {
          assert.isString(task.memory.name)
          assert.equal(task.memory.name, creep.memory.task.name)
        }
    
      })
    })
    

    它现在运行我的测试,我只需要在使用尚未模拟的东西进行测试时扩展模拟。

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 2018-06-23
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多