Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am using angular-datatables in my project and I would like to write a Jasmine/Karma unit test for it.

This is code from my controller:

 $scope.dtOptions = DTOptionsBuilder.fromSource('/api/books/')
                .withBootstrap()
                .withPaginationType('simple_numbers')
                .withDisplayLength(10)
                //.withOption('serverSide', true)
                .withOption('processing', true)
                .withOption('createdRow', createdRow);

            $scope.dtColumns = [
                DTColumnBuilder.newColumn('id').withTitle('ID'),
                DTColumnBuilder.newColumn('name').withTitle('Name')
                DTColumnBuilder.newColumn(null).withTitle('Actions').notSortable()
                    .renderWith(actionsHtml)
            ];

How can I now write a unit test for it, faking a JSON response from /api/books ?

share|improve this question

To mock an http response you use $httpBackend in your unit tests.

use $httpBackend.expect when you want the test fail if the request is not made. You can define a mock response too.

use $httpBackend.when when you want to define a mock response IF a request is made but not making the request mandatory.

CODE TO EXPLAIN ...

CONTROLLER CODE TO TEST

self.callme = function() {
    $http.get('/mypath').then(function() {
        console.log("I will be executed when flush is called if the mock API response is 2xx");
    }).catch(function() {
        console.log("I will be executed when flush is called if the mock API response is NOT 2xx");
    });
}

UNIT TEST CODE ...

$httpBackend.expectGET('/mypath').respond(200, {data:"fred"});
controller.callme();
// The THEN code has not been executed here
$httpBackend.flush();
// The THEN success code is now executed because we responded with 2xx (200)
share|improve this answer
    
I am using $httpBackend.expectGET, and $httpBackend.flush(); in the test, but I get Error: No pending request to flush !. I want to know what I need to do specificly for angular-datatables to make it work. – Wim Deblauwe Dec 23 '15 at 10:32
    
when you call flush it is saying, for any pending requests make them now. This will trigger the code inside the $http THEN statement. If your code has not made a request at the point at which you call flush then you will get the error you are experiencing. Ensure that your code has attempted to make an http request before you call flush. – danday74 Dec 23 '15 at 10:36
    
I have updated my answer with some code to illustrate. The code was written free hand so there might be syntax mistakes but you should get the idea. – danday74 Dec 23 '15 at 10:43
    
Thanks, but that was already clear to me. But I want to know specificly for angular-datatables what I need to call in the unit test code to trigger the remote call. – Wim Deblauwe Dec 23 '15 at 10:46
    
ok i see the problem now. Can you not refactor your code a bit. Instead of using the fromSource method to populate the table is there not a fromJson method or something? Then populate the JSON using $http. This will make unit testing easier. Not an ideal answer! Sorry, I've never use ng-datatables. If nothing else hopefully this discussion will help someone better understand the problem. – danday74 Dec 23 '15 at 10:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.