【发布时间】:2016-10-16 16:54:13
【问题描述】:
我已经用 Angular 编写了一些服务。检查这个PLUNKER。
在RouteService 中注入CommonService, $rootRouter, ModalService。
我坚持对这些服务进行单元测试。您可以在PLUNKER 看到示例规范文件。
编辑:无论我在 plunker 进行的什么测试都没有按预期工作。我不确定我做错了什么。
如何测试RouteService 中的goTo 和getActivePage 方法?
如何测试CommonService 中的getProperty 和setProperty 方法?
这是代码。
第一个服务是RouteService
'use strict';
angular.module('mysampleapp')
.service('RouteService',
function(CommonService, $rootRouter, ModalService) {
console.log('RRRRRRRRRRRRRRRRRRRRRRRRRRRoute');
return {
goTo: goTo,
getActivePage: getActivePage
};
function goTo(page) {
var valid = CommonService.getProperty('isValidationSuccess');
switch (page) {
case 'AboutUs':
if (valid) {
CommonService.setProperty('activeMenu', page);
$rootRouter.navigate([page]);
} else {
ModalService.openModal('Analysis Error', 'Complete Application Group configuration prior to running analysis.', 'Error');
}
break;
default:
CommonService.setProperty('activeMenu', page);
$rootRouter.navigate([page]);
break;
}
}
function getActivePage() {
return CommonService.getProperty('activeMenu');
}
});
另一个是CommonService
'use strict';
angular.module('mysampleapp')
.service('CommonService',
function() {
var obj = {
/* All page validation check before perform analysis */
isValidationSuccess: false,
/* Highlight the menu */
activeMenu: 'HomeMenu'
};
function setProperty(key, value) {
obj[key] = value;
}
function getProperty(key) {
return obj[key];
}
function getAllProperties() {
return obj;
}
return {
setProperty: setProperty,
getProperty: getProperty,
getAllProperties: getAllProperties
};
}
);
【问题讨论】:
-
嘿,我为
CommonService测试发布了我的answer。请为RouteService创建一个单独的问题。 -
在这里查看我的应用程序的真实场景stackoverflow.com/questions/40084091/…
-
@Maximus 你有机会看看新问题stackoverflow.com/questions/40084091/…
-
我明天去看看,今天来晚了)
-
您还需要帮助吗?看来红黑军团给出了正确答案
标签: javascript angularjs unit-testing jasmine karma-jasmine