【问题标题】:How do I test variables local to an Angular controller in Jasmine如何在 Jasmine 中测试 Angular 控制器的本地变量
【发布时间】:2013-01-10 23:25:28
【问题描述】:

我有一个带有局部变量的控制器

function IndexCtrl($scope) {
  var pagesById = [];
  loadPages();

  // snip

  function loadPages() {
    // pagesById gets populated
  }

  // snip
}

我想测试 pagesById 是否正确填充,但我不确定如何从我的 it() 中获取它。我不需要这个变量在 $scope 中,它只是一组中间信息,所以如果我可以避免将它添加到 $scope 中,那将是理想的。

it('scope.pages should populated based on pages data.', function() {
    $httpBackend.flush();
    expect(pagesById).toEqualData(mock_page_results);
});

给我

ReferenceError: pagesById is not defined

除了将其附加到 $scope 之外,我还有其他选择吗?

【问题讨论】:

    标签: unit-testing angularjs jasmine


    【解决方案1】:

    在您的 jasmine 规范中,首先创建控制器:

    var ctrl;
    
    beforeEach(inject(function($rootScope, $controller) {
      scope = $rootScope.$new();
      ctrl = $controller('myController', {
         $scope: scope
      });
    }));
    

    然后你可以通过ctrl.pagesById 访问它的属性。当然,您需要在控制器中使用this.pagesById,而不是使用var pagesById。

    【讨论】:

    • 这是有道理的,但我得到 TypeError: Cannot read property 'pagesById' of undefined 指的是我尝试读取 this.pagesById 的控制器中的第一行
    • this 可能在您访问它的范围内有所不同。在控制器的第一行,执行var that = this;,然后使用that.pagesById访问pagesById
    • 嘿——这开始看起来像苏斯医生的故事了。它虽然有效。我很惊讶“this”不在成员函数的范围内。哦,好的,谢谢。 +1 票和 +1 绿色检查!
    • 嘿。有关 javascript 范围的更多信息:coding.smashingmagazine.com/2009/08/01/…
    • 有时您的控制器有您不想放在示波器上的数据...
    【解决方案2】:

    这是我在 Angular 方法中测试局部变量的方法:

    //------- Function for getting value of local variables
    
    function getLocalVariable(sLocalVarName, obj, method, args){
        method = method.toString();
        let changedMethod = method.substring(0, method.lastIndexOf("}")) + ";" + "return " + sLocalVarName + "}";
        eval(' changedMethod = ' + changedMethod);
    
        return changedMethod.call(obj, args)
    }
    
    //----------- service
    
    class assignStuffService {
    
        getAvaliableStuff(shift) {
            const params = {
                agency_id: 0 ,
                start_time: shift.data.shift.start_time,
                end_time : shift.data.shift.end_time
            };
        }
    }
    
    //----------- part of spec
    
    it('should set values to "params" object props', () => {
    
                let shift = {
                    data:{
                        shift:{
                            start_time:'',
                            end_time:''
                        }
                    }
                };
    
                let params = getLocalVariable('params',
                                                assignStuffService,
                                                assignStuffService.getAvaliableStuff,
                                                shift);
    
                expect(params).toEqual({
                    agency_id: 0 ,
                    start_time: shift.data.shift.start_time,
                    end_time : shift.data.shift.end_time
                });
    
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2014-11-22
      • 2014-07-09
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      相关资源
      最近更新 更多