GeoffreyB
GeoffreyB

Reputation: 1839

Cordova & AngularJS unit testing

I'm currently developing an application based on Cordova (http://cordova.apache.org), deployed on iOS for now.

I've started to write some unit tests using jasmine, as i used to do when i developed a browser application with AngularJS (using Jasmine).

var custom_matchers = {
    toBeAFunction: function () {
        var actual = this.actual;
        var notText = this.isNot ? " not" : "";
        this.message = function() {
            return "Expected " + actual.constructor.name + notText + " to be a function";
        };
        return angular.isFunction(actual);
    }
}

describe("Test using aboutUsCtrl", function () {

    var $controller, $scope;

    beforeEach(function () {
        module("app");
        inject(function ($rootScope, _$controller_) {
            $scope = $rootScope.$new();
            $controller = _$controller_;
        });
        this.addMatchers(custom_matchers);
    })

    describe(", creating a controller", function () {

        var controller;

        beforeEach(function () {
            controller = $controller("aboutUsCtrl", {$scope: $scope});
        });

        it("should have a 'deleteAccount' function", function () {
            expect($scope.deleteAccount).toBeDefined();
            expect($scope.deleteAccount).toBeAFunction();
        });

        it("should have a 'goNext' function", function () {
            expect($scope.goNext).toBeDefined();
            expect($scope.goNext).toBeAFunction();
        })

    });

})

When i execute this piece of code, i get an error, thrown by Cordova: Function required as first argument!. A quick research makes me realize that Cordova is told to subscribe to an array of functions (Channel.subscribe) instead of only a function. Note that this error does not happen if i completely remove each test.

Is it the result of something i've been doing wrong ?

Before you ask, here is my karma config file: http://pastebin.com/JzMGgHPn

Upvotes: 2

Views: 835

Answers (1)

GeoffreyB
GeoffreyB

Reputation: 1839

My problem was really complicated to go through, since there's not a lot of stuff about the unit testing for my case. Basically, i was including cordova.js in my Karma runner, because one of my js files needed it. I figured out that this js file (PushNotification.js) didn't need to be included right here, solving my problem.

Upvotes: 1

Related Questions