mardi 26 juillet 2016

AngularJS testing with Karma

I'm an absolute noob in testing and a couple of dozens tutorials haven't helped. So please bare with me.

Could please someone help to understand a few things?

I have no problem writing tests on checking if functions are defined, but how do I go deeper and check if function are doing the right thing and returning what they are suppose to, if pretty much all of them are getting huge data amounts from DB and I can't really mock the whole dataSet. Do I still check it somehow with mockfunction?

How do I mock an argument for a function, if it's accepting a dataset as an argument? How do I test a function which uses a couple more functions inside it?

Here's a code example of one of the function I can't figure out how to test:

  vm.changeWeek = function (count) {
                    if (count > 0) {
                        vm.selectedDate = moment(vm.selectedDate).add(count, 'weeks');
                    } else {
                        vm.selectedDate = moment(vm.selectedDate).subtract(-count, 'weeks');
                    }
                    vm.selectedWeek = moment(vm.selectedDate).isoWeek();
                    if (vm.selectedWeek != vm.previousSelectedWeek || vm.selectedYear != vm.previousSelectedYear) {
                        vm.previousSelectedWeek = vm.selectedWeek;
                        vm.previousSelectedYear = vm.selectedYear;
                        vm.selectedYear = moment(vm.selectedDate).isoWeekYear();
                        vm.prevWeek = moment(vm.selectedDate).subtract(1, 'weeks').isoWeek();
                        vm.nextWeek = moment(vm.selectedDate).add(1, 'weeks').isoWeek();
                        vm.editingMode = -1;
                        if (count == -1) {
                            getHourInput(moment(vm.selectedDate).startOf('isoWeek').subtract(-count, 'weeks'), moment(vm.selectedDate).startOf('isoWeek').subtract(1, 'days'));
                        } else if (count == 1) {
                            getHourInput(moment(vm.selectedDate).startOf('isoWeek').add(count, 'weeks'), moment(vm.selectedDate).startOf('isoWeek').add(+count + 1, 'weeks').subtract(1, 'days'));
                        } else {
                            getHourInput();
                        }
                    }
                    applyHourInputFilter()
                };


  function applyHourInputFilter() {
            vm.filteredHourInputs = vm.hourinputs.filter(hourInputFilter);
            sortArr(vm.filteredHourInputs);
            changeItemName()
        }

And here's my test that gives an error:

describe('app module', function ()
{
    beforeEach(module('app'));
    describe('Testing HI Controller', function (){
    var ctrl;
    var scope;

    beforeEach(inject(function ($rootScope, $controller) {
            scope = $rootScope.$new();
            ctrl = $controller('hourinput', { $scope: scope });
        }));


        it('should spyOn changeWeek call through', function ()
        {
            spyOn(scope, 'applyHourInputFilter').and.callThrough();
            ctrl.changeWeek(1);
            expect(scope.applyHourInputFilter).toHaveBeenCalled();
        });

        });
});

output I got is:

Test Outcome:   Failed
Result Message: Error: applyHourInputFilter() method does not exist
Result StandardError:   Error: applyHourInputFilter() method does not exist
    at Object.<anonymous> 

Any advice is much appriciated.

Aucun commentaire:

Enregistrer un commentaire