我有一个利用helper类的类,我想验证它是否正确地构造了这些对象。所以,我试图在我的类中存根“构造函数”方法,但我显然做得不对:
"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看起来存根在某种程度上是有效的,因为把存根测试放在间谍用可怕的"TypeError:尝试包装已经包装的构造函数“测试错误之前。因此,清楚地弄清楚如何模拟Collaborators构造函数只是我所做错误的一半。。。我也没有正确地恢复构造函数。有什么建议吗?
发布于 2016-12-17 03:29:00
这不是我想要的解决方案,但是暂时我可能会使用这个(但是,如果你有什么建议,请把我从我自己身上拯救出来):
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。同样,我仍然希望有人能提出一种方法来对这个类进行实际的单元测试!
发布于 2016-12-17 06:59:53
我不确定这是不是我的最终答案,但我最终还是使用了proxyquire,因为这是迄今为止我找到的最好的解决方案。为了展示它是如何工作的,我将测试中的类分离到它们自己的目录中,并将测试文件放在一个子" test“目录中。这说明了proxyquire中的路径是如何工作的(我花了一些时间才弄清楚)。所以,这就是我的结论:
/Collaborator.js
"use strict"
class Collaborator {
constructor(settings) {
console.log("Don't want this to be called!")
this.settings = settings;
}
}
module.exports = Collaborator/ThingToTest.js
"use strict"
const Collaborator = require("./Collaborator")
class ThingToTest {
constructor(settings) {
this.helper = new Collaborator(settings)
}
}
module.exports = ThingToTest/test/ExampleTest.js
"use strict";
const proxyquire = require('proxyquire')
const mockCollaborator = sinon.stub();
const ThingToTest = proxyquire("../ThingToTest", { "./Collaborator" : mockCollaborator })
const assert = require("assert");
const sinon = require("sinon");
describe("ThingToTest", () => {
let settings = "all the things"
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(mockCollab.firstCall.calledWith(settings))
})
})
})
})注意proxyquire("../ThingToTest", { "./Collaborator" : mockCollaborator })中的路径如何匹配"ThingToTest“使用的内容,而不是来自测试类的路径。我希望这对其他人有所帮助,但我仍然对其他想法和建议持开放态度!
https://stackoverflow.com/questions/41190022
复制相似问题