jeudi 9 juillet 2015

Testing a function in controller and return value using Jasmine

I have this controller:

(function() {
    'use strict';
    angular.module('abc').controller(
        'abcCtrl',
        [
            'beSelected',
            function($beSelected) {
                var vm = this;
                vm.isSelectAll = true;//was false
                function selectAll() {
                    if (beSelected == "whatever") {
                        vm.isSelectAll = true;
                        return true;
                    }
                    else {
                        vm.isSelectAll = false;
                        return false;
                    }
                }
            }
        ]);
})();


My question is, how do I write a test to check if the function does its work?
I have tried the folowing:

describe('abcCtrlTest', function(){

    var $rootScope, $scope, $controller;
    var beSelected = "whatever";

    angular.module('abc', []);
    beforeEach(angular.mock.module('abc'));

    beforeEach(inject(function(_$rootScope_, _$controller_) {

        $rootScope = _$rootScope_;
        $controller = _$controller_;
        $scope = $rootScope.$new();
        $controller(
            'abcCtrl', {
                '$scope':$scope,
                'beSelected':beSelected
             });
    }));

    it("should call selectAll function and check allSelected", function() {
        $scope.vm = {};//new object
        $scope.vm.isSelectAll = false;

        spyOn($scope, "selectAll").and.callThrough();
        var ret = 0;
        ret = $scope.selectAll();
        expect($scope.selectAll).toHaveBeenCalled();

        expect($scope.vm.isSelectAll).toBeDefined();
        expect($scope.vm.isSelectAll).toBe(true);
        expect(ret).toBeDefined();
        expect(ret).toBe(true);       
    });  
});



1. Can I check the vm value in the controller(specificly to see if isSelectAll has been set)?
2.How can I check the return value of selectAll()?
Thanks!

Aucun commentaire:

Enregistrer un commentaire