【问题标题】:variable range using jasmine使用茉莉花的可变范围
【发布时间】:2015-09-23 18:43:23
【问题描述】:

我正在尝试从 angularjs 控制器测试一个函数。

注射部分:

var $scope, $controller;

beforeEach(module('sigfoxSDISApp'));
beforeEach(inject(function(_$controller_, $rootScope, _$log_, _$translate_, _DeviceService_, _$filter_, _translatableIndexFilter_){
    $controller = _$controller_;
    $scope = $rootScope.$new();
    $controller =  $controller('AcquitModalCtrl', {
        $scope: $scope,
        $log: _$log_,
        $translate: _$translate_,
        DeviceService: _DeviceService_,
        $filter_: _$filter_,
        translatableIndexFilter: _translatableIndexFilter_
    });
}));

测试:

describe('$scope.getReport', function(){
    this.success = 0;
    this.errors = 0;
    
    it('returns an object with a type, a title and a report', function(){
        var result = $scope.getReport(this.success, this.errors);
        expect(result.type).toBeDefined();
        expect(result.title).toBeDefined();
        expect(result.report).toBeDefined();
    });
    
    describe('getting type, report and title when success = 0 and errors = 1', function(){
        this.errors = 1;
        var result = $scope.getReport(this.success, this.errors);
        
        it('returns a type error', function(){
            expect(result.type).toEqual('error');
        });
        
        it('returns a report empty string', function(){
           expect(result.report).toEqual(''); 
        });
        
        it('returns a title which is not empty string', function(){
            expect(result.title).toMatch(/[a-z]/);
        });
    });
});

第一个“it”函数运行良好。 在“成功 = 0 且错误 = 1 时获取类型、报告和标题”中,我收到以下错误:“TypeError: Cannot read property 'getReport' of undefined”

因此在第一个“it”函数之后不再定义 $scope。我不明白为什么。

来自Jasmine Doc

嵌套描述块

描述的调用可以嵌套,在任何级别定义规范。这允许将套件组合为功能树。在执行规范之前,Jasmine 沿着树依次执行每个 beforeEach 函数。规范执行后,Jasmine 类似地遍历 afterEach 函数。

【问题讨论】:

    标签: angularjs unit-testing nested jasmine


    【解决方案1】:

    我只需要在每个 describe 中添加 beforeEach 来更改变量值并执行 getReport 函数,如下所示:

    describe('$scope.getReport', function(){
      beforeEach(function(){
        this.success = 0;
        this.errors = 0;
      });
    
    
      it('returns an object with a type, a title and a report', function(){
        var result = $scope.getReport(this.success, this.errors);
        expect(result.type).toBeDefined();
        expect(result.title).toBeDefined();
        expect(result.report).toBeDefined();
      });
    
      describe('getting type, report and title when success = 0 and errors = 1', function(){
        beforeEach(function(){
          this.errors = 1;
          this.result = $scope.getReport(this.success, this.errors);
        )}
    
        it('returns a type error', function(){
            expect(this.result.type).toEqual('error');
        });
    
        it('returns a report empty string', function(){
           expect(this.result.report).toEqual(''); 
        });
    
        it('returns a title which is not empty string', function(){
            expect(this.result.title).toMatch(/[a-z]/);
        });
    });
    

    });

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多