在以下模块中:

@APP.module "LeftSidebar", (LeftSidebar, APP, Backbone, Marionette) ->

API =
  initialize: ()->
    @controller = new LeftSidebar.Controller

LeftSidebar.addInitializer ()->
  API.initialize()


...我想测试在调用LeftSidebar.ControllerAPP.LeftSidebar.addInitializer()是否被初始化。我尝试使用以下规范,但是@spy.calledWithNew()返回false:

describe "LeftSidebar app", ->
  describe "initialization", ->
    beforeEach ->
      @spy = sinon.spy(APP.LeftSidebar, "Controller")
      APP.LeftSidebar.addInitializer()

  it "initializes LeftSidebar.Controller", ->
    expect(@spy.calledWithNew()).toBeTruthy()


正确的方法是什么?

最佳答案

监视initialize
您缺少要测试的初始化,请将其添加到测试中。


describe "LeftSidebar app", ->
  describe "initialization", ->
    beforeEach ->
      @spy = sinon.spy(@controller, "initialize")
      // APP.LeftSidebar.addInitializer() // this does nothing, drop it

  it "initializes LeftSidebar.Controller", ->
    new @controller();
    expect(@controller.initialize.calledOnce).toBeTruthy()

09-25 15:54