lundi 19 octobre 2015

Stubbing action request with Ember and sinon

I am trying to test a controller action defined in a mixin as below:

App.ExportMixin = Ember.Mixin.create

  isSelectionValid: (content, count) ->
    content.length > count

  actions:

    exportCSV: (content, count) ->
      return if !@isSelectionValid(content, count)
      $.fileDownload "search.csv",
        httpMethod: 'POST'
        data: 'some data'

Here is my test:

moduleFor 'mixin:export', 'Unit:Mixin:ExportMixin',

setup: ->
  @subject = Ember.Object.extend(App.ExportMixin, Ember.ControllerMixin).create()
  @normalContent = [1..1000]
  @normalTotalCount = 100

test 'action: exportCSV', ->
  Ember.run =>
    fakeServer = sinon.fakeServer.create()
    fakeServer.respondWith("POST", /.*search\.csv/,
      [ 200,
        { 'Content-Type': 'text/csv' }, '{}'
      ]
    )

    exportStub = sinon.stub @subject, 'isSelectionValid'
    @subject.send 'exportCSV', @normalContent, @normalTotalCount
    fakeServer.respond()
    ok exportStub.called
    fakeServer.restore()

The assertion fails and I have verified that the exportCSV action does not get called in my tests by setting breakpoints. Is this the correct way to test the action?




Aucun commentaire:

Enregistrer un commentaire