jasmine匿名函数的测试用例

时间:2015-11-09 11:49:10

标签: javascript angularjs jasmine karma-jasmine

我需要更改jasmine代码,但我必须为匿名函数完成一组jasmine。我是新来的。我尝试了但是,我无法完全实现。请帮我。 Html文件

@media (max-width: 800px) {
    .login {
        margin-top: 5%;
    }
}

@media (max-width: 204px) {
    .login {
        text-align: center;
        margin-right: 0%;
    }
}

js改变

<div ng-class="{ 'mainPassdownFont mainPassdownNoteLayout noteEditBackground': isBeingEdited(note), 'mainPassdownFont mainPassdownNoteLayout': isNotBeingEdited(note) }" >
    <textarea ng-attr-id="{{ 'note-' + note.PassdownNoteID }}" ng-show="isBeingEdited(note)" class="mainPassdownFont passdownTextAreaLayout">{{ note.NoteText }}</textarea>
    <div ng-hide="isBeingEdited(note)" class="mainPassdownFont passdownTextAreaLayout">
        <pre class="notePre">{{ note.NoteText }}</pre>
    </div>

    <input class="passdownNoteInlineCheckbox" ng-attr-id="{{ 'checkbox-' + note.PassdownNoteID }}" ng-model="cbModel" ng-change="checkBoxChanged(note)" ng-checked="isClaimed(note)" type="checkbox" name="notesCheckbox" value="notesCheckbox">
    <div ng-show="isClaimed(note)" class="downArrowIcon passdownNoteDownArrowLayout" ></div>
    <div ng-show="isClaimed(note)" class="passdownAnnotationFont claimTextContainerLayout" >
        <p class="claimNameLayout">{{ note.Created.By.Text }}</p>
        <p class="claimDateLayout">{{ note.Created.Date.Local | date : 'MMM d, y h:mm a' }}</p>
    </div>
    <div ng-hide="isClaimed(note)" ng-click="claimNote(note)" class="passdownAnnotationFont claimClickTextContainerLayout" >
        <p class="claimNameLayout">Click to Claim</p>
    </div>
    <div class="passdownAnnotationFont authorTextContainerLayout" >
        <p class="claimNameLayout">{{ note.LastModified.By.Text }}</p>
        <p class="claimDateLayout">{{ note.LastModified.Date.Local | date : 'MMM d, y h:mm a'  }}</p>
    </div>
    <div class="actionIconContainerLayout" >
        <div class="deleteIcon inlineActionIconLayout" ng-click="deleteNote(note)"></div>
        <div class="editIcon inlineActionIconLayout" ng-click="editNote(note)"></div>
        <div class="addIcon inlineActionIconLayout" ng-click="addSubNote(note)"></div>
    </div>
    <div class="btnforpassdownTextAreaLayout">
            <button ng-attr-id="{{ 'btn-' + note.PassdownNoteID }}" type="button" class="btn btnpassdownTextAreaLayout" ng-show="isBeingEdited(note)" ng-click="completeEdit(note)">Save</button>
            <button ng-attr-id="{{ 'btn-' + note.PassdownNoteID }}" type="button" class="btn btnpassdownTextAreaLayout" ng-show="isBeingEdited(note)" ng-click="cancelEdit(note)">Cancel</button>
    </div>
</div>          </div>
    </div>
</div>

Jasmine js文件..

app.controller('passdownCtrl',
        ['$rootScope', '$scope', '$http', '$resource',  '$location', 'PassdownNotesData','$window', 'userService',
        function($rootScope, $scope, $http, $resource, $location, PassdownNotesData, $window, userService) {            $scope.passdownNotesData={ "reviewedDateUtc" : "01-01-2000T15:00", "results": [] };
            $scope.reviewDate = null;
            $scope.indexBeingEdited = -1;
            $scope.inputfocus = false;

            $scope.filterText = "";
            // Go get trips from the server via an http Get Call.  If successful, assign the response to
            // $scope.tripData.  If errors occur, assign them to the tripData for the UI to look for
            // and provide the user some possible action.
            $scope.passdownNotesData = PassdownNotesData.getData();
            if ($scope.passdownNotesData.results.length == 0) {
                PassdownNotesData.updatePassdownNotesData()
                    .then(function(result) {
                        $scope.passdownNotesData = result;
                        $scope.reviewDate = parseBoldIQDate(result.reviewedDateUtc); //error
                    }, function(error) {
                        alert('Error getting Passdown Notes data in controller');
                    });
            } else {
                //$scope.selectedRequestIndex = 0;
                //$scope.selectedRequest = $scope.controllerRequestList.requestList[0];
            }

            parseBoldIQDate = function(dateStr) {
                var newDate = new Date();
                newDate.setUTCMonth(Number(dateStr.substr(0,2)) - 1);
                newDate.setUTCDate(Number(dateStr.substr(3,2)));
                newDate.setUTCFullYear(Number(dateStr.substr(6,4)));
                newDate.setUTCHours(Number(dateStr.substr(11,2)));
                newDate.setUTCMinutes(Number(dateStr.substr(14,2)));
                return newDate;
            };

            $scope.noteState = function (cat, checkOpen) {
                var containerName = cat + 'ContainerDiv';
                var containerElement = document.getElementById(containerName);
                if (containerElement != null) {
                    var isOpen = false;
                    if (containerElement.style.display == "block") {
                        isOpen = true;
                    }
                    return (isOpen == checkOpen);
                }
                return false;
            };
            getMaxNoteId = function() {
                // create a new note structure, add it to the list, persist it, and put it in edit mode
                var maxNoteId = 0;
                for (var i = 0; i < $scope.passdownNotesData.results.length; i++ ) {
                    if ($scope.passdownNotesData.results[i].PassdownNoteID >= maxNoteId) {
                        maxNoteId = $scope.passdownNotesData.results[i].PassdownNoteID;
                    }
                    for (var j = 0; j < $scope.passdownNotesData.results[i].SubNotes.length; j++ ) {
                        if ($scope.passdownNotesData.results[i].SubNotes[j].PassdownNoteID >= maxNoteId) {
                            maxNoteId = $scope.passdownNotesData.results[i].SubNotes[j].PassdownNoteID;
                        }
                    }
                }
                return maxNoteId;
            };

$scope.addNote = function(cat, event) {
                // create a new note structure, add it to the list (local and global), persist it, and put it in edit mode

                // create new note
                var currentDate = new Date();

                var newNote = new Object();
                newNote.PassdownNoteID = getMaxNoteId() + 1;
                newNote.Category = new Object();
                newNote.Category.Text = cat;
                newNote.NoteText = "Default Note Text";
                newNote.Created = new Object();
               // newNote.Created.ClaimStatus = "false";
                newNote.Created.By = new Object();
                newNote.Created.By.ID = 0;
                newNote.Created.By.Text = "N/A";
                newNote.Created.Date = new Object();
                newNote.Created.Date.Utc = createBoldIQDate(currentDate);
                newNote.Created.Date.Local = currentDate;

                newNote.LastModified = new Object();
                newNote.LastModified.By = new Object();
                newNote.LastModified.By.ID = 17;
                newNote.LastModified.By.Text = userService.getUser();
                newNote.LastModified.Date = new Object();
                newNote.LastModified.Date.Utc = createBoldIQDate(currentDate);
                newNote.LastModified.Date.Local = currentDate;

                newNote.SubNotes = new Array();

                // Add it to local and global lists
                //$scope.passdownNotesData.results[$scope.passdownNotesData.results.length] = newNote;
                var dataCopy = PassdownNotesData.getData();
                dataCopy.results[dataCopy.results.length] = newNote;

                // Persist the updated data to the server
                PassdownNotesData.writeUpdatesToServer(dataCopy);

                // Put the display in Edit mode
                $scope.indexBeingEdited = newNote.Id;
            };

            $scope.getNumUnclaimedForCategory = function(cat) {
                var numUnclaimed = 0;
                for (var i = 0; i < $scope.passdownNotesData.results.length; i++ ) {
                    if ($scope.passdownNotesData.results[i].Category.Text == cat) {
                        // If the main part of the note was updated, add 1 and call it good.
                        if ($scope.passdownNotesData.results[i].IsActive == "false") {
                            numUnclaimed++;
                        }
                    }
                }
                return numUnclaimed;
            };
            $scope.checkBoxChanged = function(note) {
                var elementStr = "checkbox-" + note.PassdownNoteID;
                var checkboxElement = document.getElementById(elementStr);
                var val = checkboxElement.checked;
                if (val == false) {
                    // Here we will unclaim the note
                    var dataCopy = PassdownNotesData.getDatacompleteSubNoteEdit();
                    // Update the real data 'and' our local scope copy, and write the real stuff back to the server
                    for (var i = 0; i < dataCopy.results.length; i++) {
                        if (note.PassdownNoteID == dataCopy.results[i].PassdownNoteID) {
                            console.log("inside here ", note.PassdownNoteID);
                            dataCopy.results[i].IsActive = "false";

                            //Update our local copy
                            $scope.passdownNotesData.results[i].IsActive = "false";
                        }
                    }
                    PassdownNotesData.writeUpdatesToServer(dataCopy); // $scope.passdownNotesData);

                } else {
                    $scope.claimNote(note);
                }
            };

            $scope.editSubNote = function(subNote) {
                 $scope.indexBeingEdited = subNote.PassdownNoteID;
                    var elementStr = "subNote-" + subNote.PassdownNoteID;
                 var textAreaElement = document.getElementById(elementStr);
                 $scope.subNotesText = textAreaElement.value;
            };

            $scope.isClaimed = function(note) {

                if (note.isActive == "true") {
                    return true;
                } else {
                    return false;
                }
            };
            $scope.claimNote = function(note) {
                var dataCopy = PassdownNotesData.getData();
                // Update the real data 'and' our local scope copy, and write the real stuff back to the server
                for (var i = 0; i < dataCopy.results.length; i++) {
                    if (note.PassdownNoteID == dataCopy.results[i].PassdownNoteID) {
                        dataCopy.results[i].isActive = "true";
                        dataCopy.results[i].Created.By.ID = 17;
                        dataCopy.results[i].Created.By.Text = $scope.loggedInUser;
                        var currentDate = new Date();
                        dataCopy.results[i].Created.Date.Utc = createBoldIQDate(currentDate);
                        dataCopy.results[i].Created.Date.Local = currentDate;
                        $scope.passdownNotesData.results[i].Created.Date.Utc = dataCopy.results[i].Created.Date.Utc;
                        $scope.passdownNotesData.results[i].Created.Date.Local = currentDate;
                    }
                }
                PassdownNotesData.writeUpdatesToServer(dataCopy); // $scope.passdownNotesData);
            };

            createBoldIQDate = function(date) {
                var monthStr = (date.getUTCMonth() + 1).toString();
                if (monthStr.length == 1) {
                    monthStr = "0" + monthStr;
                }
                var dateStr = (date.getUTCDate()).toString();
                if (dateStr.length == 1) {
                    dateStr = "0" + dateStr;
                }
                var yearStr = (date.getUTCFullYear()).toString();
                var hoursStr = (date.getUTCHours()).toString();
                if (hoursStr.length == 1) {
                    hoursStr = "0" + hoursStr;
                }
                var minutesStr = (date.getUTCMinutes()).toString();
                if (minutesStr.length == 1) {
                    minutesStr = "0" + minutesStr;
                }
                var dateStr = monthStr + "-" + dateStr + "-" + yearStr + "T" + hoursStr + ":" + minutesStr;

                return dateStr;
            };           
            $scope.filterTrips = function(note) {
                if (note.Category.Text == 'Trips') { return true; } else { return false; }
            };
            $scope.filterWeather = function(note) {
                if (note.Category.Text  == 'Weather') { return true; } else { return false; }
            };
            $scope.filterMaintenance = function(note) {
                if (note.Category.Text  == 'Maintenance') { return true; } else { return false; }
            };
            $scope.filterGeneral = function(note) {
                if (note.Category.Text  == 'General') { return true; } else { return false; }
            };

            $scope.toggleNotesContainer = function(cat, event) {
                var containerName = cat + 'ContainerDiv';
                var containerElement = document.getElementById(containerName);
                if (containerElement != null) {
                    if (containerElement.style.display == 'none') {
                        containerElement.style.display = 'block';
                        if (cat == "Trips") {
                            var buttonElement = document.getElementById("passdownToggleHideShowButton");
                            buttonElement.innerHTML = "Hide All";
                        }
                    } else {
                        containerElement.style.display = 'none';
                        if (cat == "Trips") {
                            var buttonElement = document.getElementById("passdownToggleHideShowButton");
                            buttonElement.innerHTML = "Show All";
                        }
                    }
                }
            };

            $scope.toggleHideShowPassdown = function() {
                // We will use the Trips container as the indicator of whether to "open All" or "Close all"

                // TODO - when we get time, we want to animate the open/close of these containers

                var tripsContainerElement = document.getElementById('TripsContainerDiv');
                if (tripsContainerElement != null) {
                    if (tripsContainerElement.style.display == 'none') {
                        // This means we need to open all the containers, and change the text on the button to
                        // "hide all"
                        var buttonElement = document.getElementById("passdownToggleHideShowButton");
                        buttonElement.innerHTML = "Hide All";

                        var weatherContainerElement = document.getElementById('WeatherContainerDiv');
                        var maintenanceContainerElement = document.getElementById('MaintenanceContainerDiv');
                        var GeneralContainerElement = document.getElementById('GeneralContainerDiv');
                        tripsContainerElement.style.display = 'block';
                        weatherContainerElement.style.display = 'block';
                        maintenanceContainerElement.style.display = 'block';
                        GeneralContainerElement.style.display = 'block';
                    } else {
                        // This means we need to close all the containers, and change the text on the button to
                        // "show all"
                        var buttonElement = document.getElementById("passdownToggleHideShowButton");
                        buttonElement.innerHTML = "Show All";

                        var weatherContainerElement = document.getElementById('WeatherContainerDiv');
                        var maintenanceContainerElement = document.getElementById('MaintenanceContainerDiv');
                        var GeneralContainerElement = document.getElementById('GeneralContainerDiv');
                        tripsContainerElement.style.display = 'none';
                        weatherContainerElement.style.display = 'none';
                        maintenanceContainerElement.style.display = 'none';
                        GeneralContainerElement.style.display = 'none';
                    }
                }
            };

            var containerElement = document.getElementById("TripsContainerDiv");
            containerElement.style.display = 'block';
            containerElement = document.getElementById("WeatherContainerDiv");
            containerElement.style.display = 'block';
            containerElement = document.getElementById("MaintenanceContainerDiv");
            containerElement.style.display = 'block';
            containerElement = document.getElementById("GeneralContainerDiv");
            containerElement.style.display = 'block';

            $scope.gotoPassdownScreen = function () {
                //$location.path('#/requests/legDetails');
            };
        }]);

2 个答案:

答案 0 :(得分:0)

describe('Controller: passdownCtrl', function() {

    var $rootScope,
        $scope,
        $controller,
        $http,
        $resource,
        PassdownNotesData,
        $window,
        userService,
        ctrl;

    // Mock required modules
    angular.mock.module('PassdownNotesDataModule');

    beforeEach(function(){
        angular.mock.module(function($provide){
            PassdownNotesData = jasmine.createSpyObj('PassdownNotesData', ['getData']);
            // Optionally if you want service return some data
            // PassdownNotesData.getData.and.returnValue({
            //  anyProperty: anyValue
            // });

            $provide.value('PassdownNotesData', PassdownNotesData);
        });
    });

    beforeEach(function(){
        angular.mock.module(function($provide){
            userService = jasmine.createSpyObj('userService', ['getUser']);
            // Optionally if you want service return some data
            // userService.getUser.and.returnValue({
            //  anyProperty: anyValue
            // });

            $provide.value('userService', userService);
        });
    });

    beforeEach(angular.mock.inject(function(_$rootScope_, _$controller_, _$http_, _$resource_, _PassdownNotesData_, _$window_, _userService_){
        $rootScope = _$rootScope_;
        $scope = _$rootScope_.$new();
        $controller = _$controller_;
        $http = _$http_;
        $resource = _$resource_;
        PassdownNotesData = _PassdownNotesData_;
        $window = _$window_;
        userService = _userService_;

        ctrl = $controller('passdownCtrl as ctrl', {
            $rootScope: $rootScope,
            $scope: $scope,
            $http: $http,
            $resource: $resource,
            PassdownNotesData: PassdownNotesData,
            $window: $window,
            userService: userService
        });
    }));

    describe('on controller initialization', function(){

        it('should set reviewDate', function(){
            expect($scope.reviewDate).toBe(null);
        });

        it('should set indexBeingEdited', function(){
            expect($scope.indexBeingEdited).toBe(-1);
        });

        it('should set inputfocus', function(){
            expect($scope.inputfocus).toBe(false);
        });

        it('should set filterText ', function(){
            expect($scope.filterText).toBe('');
        });
    });

    describe('Function: passdownNotesData', function(){

        it('should get data', function(){
            $scope.passdownNotesData();

            expect(PassdownNotesData.getData).toHaveBeenCalled();
        })

    });

    ...

});

将任何函数和属性放入控制器的viewmodel,而不是范围:

function SomeController(){
    var vm = this;

    vm.someProperty = ...;
    vm.someFunction = someFunction;

    function someFunction(){
        ....
    }
}

答案 1 :(得分:0)

app.controller('passdownCtrl', function($scope, PassdownNotesData) {
    var vm = this;

    vm.passdownNotesData = PassdownNotesData.getData();

    if (vm.passdownNotesData.results.length == 0) {
        PassdownNotesData.updatePassdownNotesData()
            .then(function(result) {
                vm.passdownNotesData = result;
                vm.reviewDate = parseBoldIQDate(result.reviewedDateUtc);
            }, function(error) {
                alert('Error getting Passdown Notes data in controller');
            });
    }

    var parseBoldIQDate = function(dateStr) {
        var newDate = new Date();
        newDate.setUTCMonth(Number(dateStr.substr(0, 2)) - 1);
        newDate.setUTCDate(Number(dateStr.substr(3, 2)));
        newDate.setUTCFullYear(Number(dateStr.substr(6, 4)));
        newDate.setUTCHours(Number(dateStr.substr(11, 2)));
        newDate.setUTCMinutes(Number(dateStr.substr(14, 2)));
        return newDate;
    };
});

describe('Controller: passdownCtrl', function(){

    var $scope,
        $controller,
        $q,
        deferred,
        PassdownNotesData,
        ctrl,
        window;

    beforeEach(angular.mock.module('requiredModule'));

    beforeEach(function(){
        angular.mock.module(function($provide){
            PassdownNotesData = jasmine.createSpyObj('PassdownNotesData', ['getData', 'updatePassdownNotesData']);

            $provide.value('PassdownNotesData', PassdownNotesData);
        });
    });

    beforeEach(function(){
        angular.mock.module(function($provide){
            window = jasmine.createSpyObj('window', ['alert']);

            $provide.value('window', window);
        });
    });

    beforeEach(inject(function(_$rootScope_, _$controller_, _$q_, _PassdownNotesData_){
        $scope = _$rootScope_.$new();
        $controller = _$controller_;
        $q = _$q_;
        PassdownNotesData = _PassdownNotesData_;
        deferred = $q.defer();

        ctrl = $controller('passdownCtrl as ctrl', {
            $scope: $scope,
            PassdownNotesData: PassdownNotesData
        });


    }));

    it('should resolve promise', function(){
        var data = {
            results: ['0']
        }

        var result = {
            name: '0',
            reviewedDateUtc: '12'
        };

        PassdownNotesData.getData.and.returnValue(data);
        PassdownNotesData.updatePassdownNotesData.and.returnValue(deferred.promise);

        deferred.resolve(result);

        $scope.$digest();

        expect(ctrl.passdownNotesData).toBe(result);

        // function parseBoldIQDate must be executed with
        // result.reviewedDateUtc value
        expect(ctrl.reviewDate).toEqual('Thu Nov 30 0 02:00:50 GMT+0200 (FLE Standard Time)');
    });

    it('should reject promise', function(){
        var data = {
            results: ['0']
        }

        PassdownNotesData.getData.and.returnValue(data);
        PassdownNotesData.updatePassdownNotesData.and.returnValue(deferred.promise);

        deferred.reject({});

        $scope.$digest();

        expect(window.alert).toHaveBeenCalledWith('Error getting Passdown Notes data in controller');
    });


});