Has anyone used Jasmine to test DataTables?

Has anyone used Jasmine to test DataTables?

ShatyUTShatyUT Posts: 6Questions: 0Answers: 0
edited April 2012 in General
I'm looking at doing some BDD on my Javascript using Jasmine: http://pivotal.github.com/jasmine/

Searching the forums for "Jasmine" returns 0 results so I'm guessing no one is doing this but thought I'd get some discussion going.

I have a JavaScript object that I use to manage my DataTable which has server-side processing enabled. I'm trying to test a custom fnRender method to make sure it is doing the correct thing. I'm successfully intercepting the ajax call made to the server but not sure how to make sure my fnRender method is getting called with my test data.

Replies

  • allanallan Posts: 63,810Questions: 1Answers: 10,516 Site admin
    I haven't come across Jasmine before, but it looks really interesting.

    Regarding your specific test, I'm not use how Jasmine works exactly, but can you put expects() assertions into the fnRender function itself? Or perhaps have it test what has been rendered (which is what I do in my own unit testing framework)?

    Allan
  • ShatyUTShatyUT Posts: 6Questions: 0Answers: 0
    I think I'm starting to figure this out. Jasmine has a concept of "spies" that allow you to do things like replace a function with your own to mock it. I was struggling with how to put spies on functions defined in the DataTables plugin. What I need to do is "mock" the ajax call to the server for data. I've gotten that to work.

    [code]
    spyOn($.fn.DataTable.defaults, "fnServerData").andCallFake(function( sUrl, aoData, fnCallback, oSettings ) {
    // construct JSON object as it would be returned from server
    var json = {
    iEcho: 1,
    iTotalRecords: 1,
    iTotalDisplayRecord: 1,
    aaData: [
    [1,2,3,4,5]
    ]
    };
    // call fnCallback to allow DataTables to do it's thing with the data
    fnCallback(json);
    }
    [/code]

    Then I should hopefully be able to put a spy on my custom fnRender method and verify it's return value.

    By the way. I want to echo what I've seen said in many posts on this forum...this is an EXCELLENT plugin that was obviously very thoughtfully developed. Thanks, and well done!

    Jason
This discussion has been closed.