문제

I'm writing specs for some Javascript classes (backbone.js views, written in Coffeescript) and I'd like to ensure that one view constructs another.

I'm attempting to do this by spying on the constructor, like so:

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()

The initialize() call on AviaView definitely causes the MatricesView constructor to be called, courtesy this line:

new Avia.MatricesView($("#tabs-3")).initialize()

It definitely works; if I run the app manually I can see the constructor being called during initialize(). However my Jasmine spec fails:

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.

I've spoken to several of my colleagues, and they agree that this should work ... can anyone suggest a good way of spying on constructors?

도움이 되었습니까?

해결책

How about:

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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top