gpt4 book ai didi

javascript - es6协作者的模拟构建

转载 作者:行者123 更新时间:2023-11-28 05:13:25 25 4
gpt4 key购买 nike

我有一个利用辅助类的类,我想验证它是否正确构造了这些对象。所以,我试图在我的类中 stub “构造函数”方法,但我显然做得不对:

"use strict";

class Collaborator {
constructor(settings) {
console.log("Don't want this to be called!")
this.settings = settings;
}
}

class ThingToTest {
constructor(settings) {
this.helper = new Collaborator(settings);
}
}

const assert = require("assert");
const sinon = require("sinon");

describe("ThingToTest", () => {
let settings = "all the things"

context("spying on constructor", () => {
let spy = sinon.spy(Collaborator, "constructor")
after(() => spy.restore())
describe("constructor", () => {
it("creates a Collaborator with provided settings", () => {
new ThingToTest(settings);
sinon.assert.calledWith(spy, settings)
})
})
})

context("spying on prototype constructor", () => {
let spy = sinon.spy(Collaborator.prototype, "constructor")
after(() => spy.restore())
describe("constructor", () => {
it("creates a Collaborator with provided settings", () => {
new ThingToTest(settings);
sinon.assert.calledWith(spy, settings)
})
})
})

context("stub constructor", () => {
before(() => {
sinon.stub(Collaborator, "constructor", (settings) => {
console.log("This should be called so we can inspect", settings);
})
})
after(() => { Collaborator.constructor.restore() })
describe("constructor", () => {
it("creates a Collaborator with provided settings", () => {
new ThingToTest(settings);
})
})
})

context("stub prototype constructor", () => {
before(() => {
sinon.stub(Collaborator.prototype, "constructor", (settings) => {
console.log("This should be called so we can inspect", settings);
})
})
after(() => { Collaborator.prototype.constructor.restore() })
describe("constructor", () => {
it("creates a Collaborator with provided settings", () => {
new ThingToTest(settings);
})
})
})

})

运行此命令会产生这些(不需要的)结果:

ThingToTest
spying on constructor
constructor
Don't want this to be called!
1) creates a Collaborator with provided settings
spying on prototype constructor
constructor
Don't want this to be called!
2) creates a Collaborator with provided settings
stub constructor
constructor
Don't want this to be called!
✓ creates a Collaborator with provided settings
stub prototype constructor
constructor
Don't want this to be called!
✓ creates a Collaborator with provided settings

似乎 stub 是有效的,因为将 stub 测试放在 spy 测试错误之前,并出现可怕的“TypeError:试图包装已经包装的构造函数”。因此,清楚地弄清楚如何模拟 Collaborators 构造函数只是我做错的事情的一半。 。 。我也没有正确恢复构造函数。有什么建议吗?

最佳答案

不是我想要的解决方案,但是目前我可能最终会使用它(但是请,如果您有建议,请救救我):

context("checking Collaborator in a more integration style test", () => {
describe("constructor", () => {
it("creates a Collaborator with provided settings", () => {
let thing = new ThingToTest(settings);
assert.equal(thing.helper.settings, settings)
})
})
})

这将通过并验证协作者是否设置了正确的设置。但现在如果我想重构 Collaborator 构造函数,我将破坏 ThingToTest。再次强调,我仍然希望有人能提出一种方法来实际对此类进行单元测试!

关于javascript - es6协作者的模拟构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41190022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com