Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Marionette Modules in isolation with Jasmine

I have a Marionette project, similar to setup described in http://www.backbonerails.com/ I am using Rails, Coffeescript and Jasmine/Sinon for specs.

I'm having problems testing modules in isolation. One example is the router:

@MyApp.module "DashboardApp", (DashboardApp, App, Backbone, Marionette, $, _) ->

  class DashboardApp.Router extends Marionette.AppRouter
    appRoutes:
      "dashboard" : "showDashboard"

  API =
    showDashboard: ->
      DashboardApp.Show.Controller.showDashboard()

  App.addInitializer ->
    new DashboardApp.Router
      controller: API

I am not sure how to test this in isolation from the App (window.MyApp). I would like to basically trigger the "#dashboard" route and assert that showDashboard gets called, without having to let the whole application take part in this. We're using the Rails assets pipeline (so no RequireJS), so all the JS files are loaded on start. What I think would work fine is to somehow mock the App object used in this module, but I am open to any solution.

The other problem I have similarly is testing Marionette commands and reqres, for example in a view I have

App.execute("navigate:root")

Again the problem is that I don't want to have the real application take part in the spec, I want to e.g. make a new Marionette.Application just for the spec. The view is in a module that is defined the same way as I showed in the first code example.

Basically I want to avoid using the real application in specs, and use either a mock or (probably better/easier) just a new Marionette.Application. So this would be useful for me in all cases where things go through the application object, for example Wreqr stuff, initializers for routers etc.

like image 440
mrbrdo Avatar asked Oct 05 '22 12:10

mrbrdo


1 Answers

You should stub with sinonJs.

First test should instantiate the Router and then stub the Controller showDashboard method.

@router = new App.DashboardApp.Router
@showDashboardStub = sinon.stub @router.controller, 'showDashboard'
@router.navigate "dashboard", {trigger: true}

and the test that it was called

expect(@showDashboardStub).toHaveBeenCalled()

The second test depends if you want to test command being called for execution or the command being handled. First case you instantiate the executor and stub App.execute, second case you instantiate the class that handles the command and call App.execute.

like image 72
Giorgio Bozio Avatar answered Oct 12 '22 13:10

Giorgio Bozio