Skip to content

Instantly share code, notes, and snippets.

@richhollis
Created August 29, 2014 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richhollis/4185765d817b106c3aab to your computer and use it in GitHub Desktop.
Save richhollis/4185765d817b106c3aab to your computer and use it in GitHub Desktop.
Test backbone pubSub event triggers call their expected functions in jasmine spec
class BackbonePubSub
@verifyEvent: (klass, name, methodName, callMethod = "registerEvents") ->
match = false
oldFn = Backbone.pubSub.on # get existing pubSub.on method
class KlassNoConstructor extends klass # extend original class so we can disable constructor
constructor: -> # zap the constructor of the target class
instance = new KlassNoConstructor
Backbone.pubSub.on = (triggerName, method) ->
match = true if triggerName == name and method == instance[methodName] # are we triggering on the right method?
instance[callMethod]() # call the event registration method
Backbone.pubSub.on = oldFn # restore original Backbone pubSub.on method
match
window.BackbonePubSub = BackbonePubSub # export to global namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment