0

我目前正在开始测试我的 javascipt 代码,但我现在遇到了一个我无法解决的问题。我有一个主干应用程序(AMD/requirejs 驱动),我使用 Mocha(Sinon、Chai、...)进行 BDD 测试——这基本上完成了我的设置。

假设我们正在谈论这个类

class MyApp extends App
    init: ->
        @initcontrollers()

    initControllers: ->
        new HeaderController()
        new NavController()

对于第一种方法init,我可以编写以下测试用例

before ...

describe 'init', ->

    it 'should call @initControllers', ->
        spy = sinon.spy(@myInstance, 'initControllers')
        @myInstance.init()
        expect(spy.called).toBeTruthy()

这很好用。但现在我想测试一下,如果第二种方法initControllers实际上创建了新的实例HeaderControllerNavController

我怎样才能做到这一点?我现在被困住了,我有点困惑,因为我开始认为这不是调用这些控制器的正确方法。

任何帮助表示赞赏

4

1 回答 1

0

我真的很困惑,但@mu-is-to-short 可能给了我正确的提示

我现在是这样做的:

describe '@initControllers', ->
    it 'should call HeaderController', ->
        headerController = new HeaderController()
        spy = sinon.spy(headerController.__proto__, 'initialize')
        @myInstance.initControllers()
        expect(spy.calledOnce).toByTruthy()

它对我有用,那是正确的方法吗?不管怎么说,还是要谢谢你

于 2012-06-04T16:23:19.967 回答