【发布时间】:2014-01-20 12:19:46
【问题描述】:
我已经用 jasmine 测试了我的 javascript 代码,但是当我看到我的 jenkins 单元测试覆盖率时,我有一些行没有标记为绿色(未测试)。所以这就是我问我的问题的原因:
--> 假设 tagStyle.length = 0
$scope.isTagStylesNotEmpty = function() {
if($scope.tagStyles.length >= 0) /* This line is RED (not covered ) */
{
return true;/* This line is Green OK*/
}
}
--> 我的测试如下:
it('Unit test isTagStylesNotEmpty()', inject(function($httpBackend) {
expect($scope.isTagStylesNotEmpty).toBeDefined();
$scope.isTagStylesNotEmpty();
expect($scope.tagStyles.length).toBe(0);
}));
关于这一点有什么想法吗?
第二个问题与第一个问题相同,但有点复杂:
我有以下 Javascript 文件,我想用 jasmine 进行单元测试:
$scope.ajouterProfilTag = function(lastDocId) {
$scope.tagStyles.forEach(function(item) {
/*Not covered from here (by jenkins )--------------------*/
var profilTag = {
tag: item.id_tag,
texte: item.style,
profil: lastDocId,
tagName: item.label,
police: item.police,
taille: item.taille,
interligne: item.interligne ,
styleValue: item.styleValue,
};
$http.post('/ajouterProfilTag', profilTag)
.success(function(data) {
$scope.profilTagFlag = data; /* unit tests */
$scope.afficherProfils();
$scope.profilTag = {};
$scope.tagStyles.length = 0;
$scope.tagStyles = [];
/*Until here --------------------*/
});
});
$scope.tagList = {};
$scope.policeList = {};
$scope.tailleList = {};
$scope.interligneList = {};
$scope.weightList = {};
};
我的单元测试是这样的:
var profil = {
_id: "52d8f928548367ee2d000006",
photo: "./files/profilImage.jpg",
descriptif: "descriptif3",
niveauScolaire: "CM2",
type: "Dyslexie N2",
nom: "Nom3"
};
var profilTag = {
_id: "52d8f928548367ee2d000006",
tag: "tag",
texte: "texte",
profil: "profil",
tagName: "tagName",
police: "Arial",
taille: "eight",
interligne: "fourteen",
styleValue: "Bold"
}
beforeEach(inject(function($controller, $rootScope, $httpBackend) {
$scope = $rootScope.$new();
controller = $controller('ProfilesCtrl', {
$scope: $scope
});
$httpBackend.whenPOST('/ajouterProfilTag').respond(profilTag);
}));
it('ProfilesCtrl:ajouterProfilTag should set ajouterProfilTag function', inject(function($httpBackend) {
expect($scope.ajouterProfilTag).toBeDefined();
$scope.ajouterProfilTag(profil._id);
$httpBackend.flush();
expect($scope.profilTagFlag).toEqual(profilTag);
}));
关于如何对我的第二种方法 (AjouterProfilTag) 进行单元测试的任何想法?在此先感谢
【问题讨论】:
-
在你的第一部分。你是如何设法为 [something]Controller 填充 $scope 的?从你的代码我猜它是未定义的。
-
关于你的第二部分,我猜它是一个控制器,是吗?
-
@Dalorzo 是的,它是一个控制器,我正在尝试使用 jasmine 进行单元测试。我在每个
$scope = $rootScope.$new(); controller = $controller('ProfilesCtrl', { $scope: $scope });
标签: javascript angularjs unit-testing jasmine karma-jasmine