0
我想對if語句執行單元測試,以便代碼覆蓋的分支部分提供良好的結果。我沒有得到這樣的想法,因爲究竟應該在下面單元測試if if else語句角度js
Controller.js代碼
(function() {
"use strict";
angular
.module("app.login")
.controller("LoginController", LoginController);
LoginController.$inject = ["$scope", "$http", "$state", "$rootScope", "logger", "$location"];
/**
* @ngdoc controller
* @name app.login.controller
* @memberof app.login
* @summary
* This is the controller for the login module
*
* @param {Object} $scope
* @param {Object} $http
* @param {Object} $state
* @param {Object} $rootScope
* @param {Function} logger
* @constructor
*/
function LoginController ($scope, $http, $state, $rootScope, logger, $location) {
var vm = this;
this.cancel = $scope.$dismiss;
logger.info("Started LoginController");
/**
* login function
*
* @memberof app.login.controller
*/
this.login = function() {
if ($scope._username == "admin" && $scope._password == "password") {
$state.go("main.login.success");
} else {
logger.error("Login Unsuccessful");
}
};
}
})();
testing.js
describe("LoginController", function() {
var $scope;
var controller;
var logger;
var $cookies;
var temp;
beforeEach(module("app.login"));
beforeEach(inject(function ($controller, _$rootScope_, _logger_, _$cookies_) {
$scope = _$rootScope_.$new();
controller = $controller("LoginController", {
$scope: $scope,
logger: _logger_
});
$scope.$digest();
}));
it("admin should be the username", function() {
$scope._username = "admin";
expect($scope._username).toEqual("admin");
});
it("is username is admin", function() {
expect(controller.login).toBe(true);
});
我都嘗試,但它不覆蓋的if-else分支呢。任何人都可以請指出我在上面的代碼中缺少的是什麼。謝謝!