我正在为一些 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()
. 但是我的茉莉花规格失败了:
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.
我已经和我的几位同事谈过,他们同意这应该可行……有人能建议一种监视构造函数的好方法吗?