我用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);
}));
Anyh关于如何对第二种方法(AjouterProfilTag)进行单元测试的想法?提前谢谢
答案 0 :(得分:0)
只是一个猜测,因为我没有看到控制器,但似乎$ scope.tagStyles只是一个空对象。这意味着forEach将无需迭代,因此它永远不会调用帖子。 我会尝试类似的东西:
$scope.tagStyles = [{}];
前
$scope.ajouterProfilTag(profil._id);