在以下模块中:
@APP.module "LeftSidebar", (LeftSidebar, APP, Backbone, Marionette) ->
API =
initialize: ()->
@controller = new LeftSidebar.Controller
LeftSidebar.addInitializer ()->
API.initialize()
...我想测试LeftSidebar.Controller
在调用APP.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()
这样做的正确方法是什么?
答案 0 :(得分:1)
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()