Is there a simple way to inject a Backbone.Marionette Application instance into Backbone.Marionette Controller on requirejs?

78 views
Skip to first unread message

gumaflux

unread,
Apr 22, 2014, 7:48:51 AM4/22/14
to backbone-...@googlegroups.com

I have a Backbone.Marionette (BBM) Controller with some event handlers that reference the BBM Application object. The app is built on requirejs, and I am using SinonJS stubs in the majority of my mocha tests which works fine. The only issue I am seeing is to how stub the methods on the BBM Application instance. The actual stubbing works fine, but the controller is executed with the BBM Application instance which is required in the dependencies. When I stub methods on the show method on the regions on the MyApp intance it works fine, it's on the methods directly on the BBM Application that don't get stubbed in the Controller (SUT).

(Describing this in coffeescript - if coffee makes you queasy just skip it)

define  ["app"], (myApp) ->

  MyApp.module("MyModule", MyModule, MyApp ...

    MyApp.Controller = ->
      myView = new MyView
      myView.on "event:fired", ->
        MyApp.doStuff()

I'm trying to stub MyApp.doStuff() in a simple way for my mocha tests.

Question: is this best solved refactoring the controller and somehow inject the MyApp, if so best approach? Or is there a way to "just" set a mocked MyApp on the MyModule or Controller? Or is there a better approach?

Would be very grateful for any ideas / answers! Thanks

gumaflux

unread,
Apr 22, 2014, 7:54:37 AM4/22/14
to backbone-...@googlegroups.com
define  ["app"], (myApp) ->

  MyApp.module("MyModule", MyModule, MyApp ...


    MyModule.Controller = ->

      myView = new MyView
      myView.on "event:fired", ->
        MyApp.doStuff()
Corrected a little typo
Reply all
Reply to author
Forward
0 new messages