首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >es6 collaborator的模拟构造

es6 collaborator的模拟构造
EN

Stack Overflow用户
提问于 2016-12-17 02:03:18
回答 2查看 222关注 0票数 1

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

代码语言:javascript
复制
"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);
      })
    })
  })

})

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

代码语言:javascript
复制
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构造函数只是我所做错误的一半。。。我也没有正确地恢复构造函数。有什么建议吗?

EN

回答 2

Stack Overflow用户

发布于 2016-12-17 03:29:00

这不是我想要的解决方案,但是暂时我可能会使用这个(但是,如果你有什么建议,请把我从我自己身上拯救出来):

代码语言:javascript
复制
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。同样,我仍然希望有人能提出一种方法来对这个类进行实际的单元测试!

票数 0
EN

Stack Overflow用户

发布于 2016-12-17 06:59:53

我不确定这是不是我的最终答案,但我最终还是使用了proxyquire,因为这是迄今为止我找到的最好的解决方案。为了展示它是如何工作的,我将测试中的类分离到它们自己的目录中,并将测试文件放在一个子" test“目录中。这说明了proxyquire中的路径是如何工作的(我花了一些时间才弄清楚)。所以,这就是我的结论:

/Collaborator.js

代码语言:javascript
复制
"use strict"

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

module.exports = Collaborator

/ThingToTest.js

代码语言:javascript
复制
"use strict"

const Collaborator = require("./Collaborator")

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

module.exports = ThingToTest

/test/ExampleTest.js

代码语言:javascript
复制
"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“使用的内容,而不是来自测试类的路径。我希望这对其他人有所帮助,但我仍然对其他想法和建议持开放态度!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41190022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档