vendredi 9 janvier 2015

Ember.run.later in testing and testing errors

I built a simple flash message service in an Ember-CLI application based on this post: http://ift.tt/11GaCsH


As the code in the post shows, the service relies on Ember.run.later to destroy the messages after a specified period of time.


I use this service in various parts of my application, on route and controller levels.


I wanted to test this functionality with some integration tests. For example, on the Application route, a user can submit a search query to the backend. If the query isn't properly formed, then I throw an error using the flash message service. Similarly, I can check for if authentication was successful:



`import Ember from 'ember'`
`import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin'`

ApplicationRoute = Ember.Route.extend ApplicationRouteMixin,
setupController: (ctrl) ->
flashes = @get 'flashes'
ctrl.set 'flashes', flashes
actions:
error: (e) ->
if e.jqXHR and e.jqXHR.status is 404
@get('flashes').warning('no data from the server')
sessionAuthenticationFailed: (e) ->
@get('flashes').danger('another relevant error message')

`export default ApplicationRoute`


To test this, I would simulate the desired behavior, and then check for the existence of the message:



test 'checking if the error appears on a 404', ->
# series of steps that lead to a 404
andThen ->
equal(find('.ember-notify').text(), 'no data from the server')


Where 'ember-notify' is the class name of the div containing the flashed message.


I see the message pop up in the testing container as desired - and then the tests 'pause' until the message disappears. Which means that my test ends up failing, because it checks for the existence of the div after it disappears.


My understanding is that this is explained in this thread: http://ift.tt/1tTThKg


e.g., "Ember-testing checks for scheduled timers to finish before tearing down your app."


So my testing environment will always wait for Ember.run.later to finish.


Any thoughts / ideas on how to approach this? Is there a better way to test error events on the route level?


My thought was that I could directly test whether or not the error event was called on the Application route, for example, but I don't know how to do this (and how would I extend this pattern to testing on controllers, where I'm also using the flash message service?)


Thanks!


Aucun commentaire:

Enregistrer un commentaire