Search code examples
ember.jsember-router

How can I directly invoke an event handler on a route in Emberjs?


I'm hoping to stub/spy on a route's event handler in ember.js using my testing framework of choice, jasmine. Usually this involves overwriting the function of interest with a spy, which requires access to the object on which the method is defined:

spy = spyOn(someObject, "methodOnThatObject")

But in Ember, my event handlers for my routes are defined as follows:

App.ActivityRoute = Ember.Route.extend({
  events: {
    show: function(context) {
    }
  }
});

I would like to stub the function show, but I don't know how to get the object on which it is eventually defined? Or is it ever defined on an object? Perhaps it's invoked with #call or #apply? If so, how does one stub this?

I've tried digging around the source, but didn't manage to figure out how this is handled. Any pointers to where I should look in the source would also be helpful.

Cheers, Kevin


Solution

  • Silly me. I can just do the following:

    route = App.__container__.lookup('route:myRoute')
    spy = spyOn(route.get('events'), 'show')
    controller.send('show')
    expect(spy).toHaveBeenCalled()
    

    And that works.