茉莉花单元测试中的响应未定义

时间:2018-02-07 10:46:50

标签: javascript angular unit-testing typescript jasmine

测试用例

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<h1>better if you use model</h1>
     <div ng-repeat="tab in tabs2">
        <div ng-if="$index == 1" ng-init="randomFnc2(tab)">
          {{$index}}
          <input type="text" value="" placeholder="{{tab.placeholder}}"/>
        </div>
        <div ng-if="$index != 1">{{$index}}</div>
     </div>

<h1>also you can</h1>

     <div ng-repeat="tab in tabs">
        <div ng-if="$index == 1" ng-init="randomFnc(tab)">
          {{$index}}
          <input type="text" value="" placeholder="{{placeholder}}"/>
        </div>
        <div ng-if="$index != 1">{{$index}}</div>
     </div>
     
</div>
</div>

这是我尝试测试的组件方法

it('SaveVersionMainScreen', fakeAsync(() => {
        let response = comp.SaveVersionMainScreen();// return undefined instead of false
        fixture.detectChanges();
        expect(response).toEqual(false);
    }));

当我运行我的测试用例时,SaveVersionMainScreen() { let element = this.commonValidation(true);// return null if (element) { return false; }else{ return true } } 返回null。因此if条件传递this.commonValidation(true)true返回SaveVersionMainScreen()。但我在测试用例中得到了false

undefined

1 个答案:

答案 0 :(得分:0)

我不知道原因。但是当我在if&amp;的外面使用return时否则声明,然后它正常工作

SaveVersionMainScreen() {
        let element = this.commonValidation(true);// return null
        if (element) {            
            return false;
        }else{
          return true  // here i have some logic's.
        }
       return true
     }