perseverance
perseverance

Reputation: 6612

How do you test a Backbone Marionnette method inside of onShow in Jasmine?

I'm trying to test a view in Backbone Marionette but the onShow() never gets called so I can't test a method that is being called in that method.

views/test.coffee

onShow: () ->
  debugger # this never happens when I run the Jasmine tests
  alert "HI"

spec/javascripts/views/test_spec.coffee

  describe 'a test', ->

    beforeEach ->
      @view  = new window.TestView
      @view.render()

    it "does something", ->
      # not important

Upvotes: 2

Views: 560

Answers (2)

user3694489
user3694489

Reputation: 21

You can also trigger onShow. Sorry no CoffeeScript.

view.triggerMethod("show");

Upvotes: 2

Cubed Eye
Cubed Eye

Reputation: 5631

onShow() generally only gets called when you show it inside a region. There a two options you have for testing.

1) call onShow manually after render:

@view.render();
@view.onShow();

2) show the view inside a region:

You can just make a new region inside your test file, just add a detached DOM element if you don't need to use the DOM, otherwise you can just make an element and put it in the DOM.

Sidenote*** I don't know CoffeeScript, so the following might not be syntactically correct!

beforeEach ->
    @view  = new window.TestView
    @testRegion = new Backbone.Marionette.Region({el: document.createElement('div')})
    @testRegion.show(@view)

Upvotes: 3

Related Questions