简体   繁体   English

AngularJS前端Jasmine单元测试问题

[英]Angularjs frontend Jasmine unit test issue

i have tested my javascript code with jasmine but when i see my jenkins unit tests coverage, i have some rows that are not marked green ( not tested ) . 我已经用茉莉花测试了我的javascript代码,但是当我看到jenkins单元测试覆盖率时,我有一些未标记为绿色的行(未测试)。 So this is why i'm asking my question : 所以这就是为什么我问我的问题:

--> Assuming that tagStyle.length = 0 ->假设tagStyle.length = 0

                $scope.isTagStylesNotEmpty = function() {
                    if($scope.tagStyles.length >= 0) /* This line is RED (not covered ) */
                    {
                            return true;/* This line is Green OK*/
                    }
                }

--> My test is the following : ->我的测试如下:

  it('Unit test isTagStylesNotEmpty()', inject(function($httpBackend) {
    expect($scope.isTagStylesNotEmpty).toBeDefined();
    $scope.isTagStylesNotEmpty();
    expect($scope.tagStyles.length).toBe(0);
    }));

Any ideas about that point ? 关于这一点有什么想法吗?

Second question is the same as the first one but a little bit complicated : 第二个问题与第一个问题相同,但有点复杂:

i have the following Javascript file that i want to unit test with jasmine : 我有以下要使用茉莉进行单元测试的Javascript文件:

    $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 = {};

};

my unit test is something like this : 我的单元测试是这样的:

 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 ideas about how do i have to unit test my second method (AjouterProfilTag) ?? 关于如何必须对第二种方法(AjouterProfilTag)进行单元测试的想法? thanks in advance 提前致谢

Just a guess, since I don't see the controller, but it seems like $scope.tagStyles is just an empty object. 只是一个猜测,因为我没有看到控制器,但是$ scope.tagStyles似乎只是一个空对象。 This means that the forEach will have nothing to iterate over, so it will never call the post. 这意味着forEach将没有任何要迭代的内容,因此它将永远不会发帖。 I would try something like: 我会尝试类似的东西:

$scope.tagStyles = [{}];

before 之前

$scope.ajouterProfilTag(profil._id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM