我正在编写一些Javascript类的规范(backbone.js视图,用Coffeescript编写),我想确保一个视图构建另一个视图。
我试图通过监视构造函数来实现这一点,如下所示:
describe 'Avia.AviaView', ->
beforeEach ->
@aviaView = new Avia.AviaView()
@matricesView = new Backbone.View()
spyOn(Avia.MatricesView, 'constructor').andReturn @matricesView
describe 'initialize', ->
beforeEach ->
@aviaView.initialize()
it 'creates a new MatricesView ', ->
expect(Avia.MatricesView.constructor).toHaveBeenCalledOnce()
对AviaView的initialize()
调用肯定会导致调用MatricesView构造函数,这句话非常友好:
new Avia.MatricesView($("#tabs-3")).initialize()
绝对有效;如果我手动运行应用程序,我可以看到在initialize()
期间调用的构造函数。但是我的Jasmine规范失败了:
Running Jasmine specs...
F
Avia.AviaView initialize creates a new MatricesView . (/Users/dev/avia/spec/javascripts/views/avia_view_spec.js.coffee:13)
Expected constructor to be called exactly once, but was called '0' times (line ~14)
expect(Avia.MatricesView.constructor).toHaveBeenCalledOnce();
FAIL: 1 test, 1 failure, 0.008 secs.
我和几位同事说过,他们同意这个应该工作......任何人都可以建议一种监视建设者的好方法吗?
答案 0 :(得分:5)
怎么样:
describe 'Avia.AviaView', ->
beforeEach ->
@aviaView = new Avia.AviaView()
@matricesView = new Backbone.View()
spyOn(Avia, 'MatricesView').andReturn @matricesView
describe 'initialize', ->
beforeEach ->
@aviaView.initialize()
it 'creates a new MatricesView ', ->
expect(Avia.MatricesView).toHaveBeenCalledOnce()