Odd result for spying on Mojo.Controller.SceneController.listen/stopListening

18 views
Skip to first unread message

Naleag Deco

unread,
Nov 22, 2010, 6:53:11 PM11/22/10
to jasmine-webos
I'm seeing some weird behaviour when trying to spy on listen/
stopListening calls.

My spec looks something like ... (I apologize for spelling mistakes)

var assistant;

beforeEach(function () {
assistant =
jasmine.webos.createStubSceneAssistant('TestView', arg);

});

it('should pass this test', function() {
spyOn(assistant.controller, 'listen');
spyOn(assistant.controller, 'stopListening');

assistant.setup();

expect(assistant.controller.listen).toHaveBeenCalledWith("an_element_id",
Mojo.Event.listTap,
assistant.a_function.bindAsEventListener(assistant));
...
});


My scene's setup function contains a call to
"this.controller.listen("an_element_id", Mojo.Event.listTap,
this.a_function.bindAsEventListener(this));" and there is a similar
call in my cleanup code.

The code appears to work, but Jasmine reports an error similar to:
"Expected spy to have been called with ['an_element_id', 'mojo-list-
tap', Function ] but was called with [['story_list, 'mojo-list-tap',
Function ]]"


I am unable to figure out why the "real" function call in Jasmine
winds up being wrapped in another array, and whether that's what
happens in production code or just via the framework. I have tried
playing with Mojo.Event.listen/stopListening and with using bind
instead of bindAsEventListener.

Is there a better way to test these calls?

DWF

unread,
Nov 22, 2010, 9:44:26 PM11/22/10
to jasmine-webos
The problem you're running into is that #bind and #bindAsEventListener
return different instances of anonymous functions with every call. So
you're comparing two different functions (even though they are
'equivalent').

Two suggestions:

1) Don't care which function is called and match jasmine.any(Function)
for the 3rd parameter
2) Bind the function on the assistant constructor or #setup and save
it off, then compare against that property

--dwf
Reply all
Reply to author
Forward
0 new messages