Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a spyOn analogue in QUnit?

I'm writing specs for different test cases for Jasmine and QUnit to compare them and they looked the same before I needed to write a test to check if an event is binded to an element.

Event binding looks like

$('.page').live('click', function() { page_clicked( $(this) ) });

page_clicked is a private method but it calls for a public method of another module.

Here is a Jasmine spec:

it('should bind events to pages', function() {
    spyOn( search, 'get_results' );

    $('.page:eq(0)').trigger('click');

    expect( search.get_results ).toHaveBeenCalled();
});

This test works. Now I'm trying to write the same test for QUnit and can't find anything similar to spyOn. How to write this test for QUnit?

like image 525
Daniel J F Avatar asked Jan 15 '12 12:01

Daniel J F


1 Answers

Its cause QUnit doesn't have spies or mocks. But you can use the Sinon.JS mocking framework. Your test should look like this using sinon spy:

var spy = sinon.spy(search, 'get_results');
sinon.assert.calledOnce(spy);
like image 118
Andreas Köberle Avatar answered Sep 28 '22 17:09

Andreas Köberle