1

我正在尝试为循环中的 if 条件编写一个测试用例,但是我没有将它作为过滤器的一部分。有什么方法可以测试循环中的 if 条件。我的测试用例根本不执行这行代码。

下面是我的控制器代码

   $scope.init = function()
   {
for (var i = 0; i < $scope.tempdata.length; i++) { //mapping of the status   to text

            if ($scope.tempdata[i].Status == 'U') {
                    $scope.statusText = 'text1';

                }
                if ($scope.tempdata[i].Status == 'A' || $scope.tempdata[i].Status == 'W') {
                    $scope.statusText = 'text2';
                }
                if ($scope.tempdata[i].Status == 'F') {
                    $scope.statusText = 'text3';
                }
                if ($scope.tempdata[i].Status == 'P') {
                    $scope.statusText = 'text4';
                }
                if ($scope.tempdata[i].Status == 'E') {
                    $scope.statusText = 'text5';
                }
                if ($scope.tempdata[i].Status == 'S') {
                    $scope.statusText = 'text6';
                }
                  }
          }

下面是我的测试用例

 it('should set the status', function() {
   scope.responseSet = true;
   var mockTempData =[{'Status': 'F'}];
    scope.tempdata = mockTempData
    scope.init();  
    expect(scope.statusText).toBe(text3);
});

当我运行业力时,我的测试用例失败,Expected '' to be text3.

4

1 回答 1

2

如果您展示了如何将范围“注入”到控制器测试中,则更容易判断。但是,我发现调试我的单元测试以仔细检查它们并确保一切都符合我对每个步骤的期望是非常有帮助的。

你还应该有你的最后一行

expect(scope.statusText).toBe('text3');

请注意,测试代码中的 text3 应更改为 'text3',因为这是实际控制器中的字符串,而不是变量名。

于 2015-11-11T06:26:17.943 回答