我想对 if else 语句执行单元测试,以便代码覆盖的分支部分给出良好的结果。我不明白应该在下面的代码中测试什么
控制器.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");
}
};
}
})();
测试.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 分支。谁能指出我在上面的代码中缺少什么。谢谢!