2015-10-15 48 views
0

所以我正在运行AngularJS教程,我在第5章中,并且遇到了这个Karma测试。它看起来像这样:Karma茉莉花测试期间两次使用describe()

'use strict'; 

/* jasmine specs for controllers go here */ 
describe('PhoneCat controllers', function() { 

    describe('PhoneListCtrl', function(){ 
    var scope, ctrl, $httpBackend; 

    beforeEach(module('phonecatApp')); 
    beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) { 
     $httpBackend = _$httpBackend_; 
     $httpBackend.expectGET('phones/phones.json'). 
      respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]); 

     scope = $rootScope.$new(); 
     ctrl = $controller('PhoneListCtrl3', {$scope: scope}); 
    })); 


    it('should create "phones" model with 2 phones fetched from xhr', function() { 
     expect(scope.phonez).toBeUndefined(); 
     $httpBackend.flush(); 

     expect(scope.phonez).toEqual([{name: 'Nexus S'}, 
            {name: 'Motorola DROID'}]); 
    }); 


    it('should set the default value of orderProp model', function() { 
     expect(scope.orderProp).toBe('age'); 
    }); 
    }); 
}); 

,它正在测试的代码看起来是这样的:

'use strict'; 

/* Controllers */ 

var phonecatApp1 = angular.module('phonecatApp', []); 

phonecatApp1.controller('PhoneListCtrl3', ['$scope', '$http', function($scope, $http) { 
    $http.get('phones/phones.json').success(function(data) { 
     $scope.phonez = data; 
    }).error(function(data, status) { 
     $scope.err = "Cannot make connection to data. Reason: " + status + data; 
     console.log("Error status : " + status); 
    }); 

    $scope.orderProp = 'age'; 
}]); 

我的问题是,为什么叫形容两次?我从测试中删除了初始描述,即describe('PhoneCat controllers', function(),测试通过没有任何问题。

谢谢你的时间。

回答

2

描述方法增加了一个测试套件,它只是一个测试用例分离 -

见下面的例子 -

describe('PhoneCat controllers', function() { 
 

 
    describe('PhoneListCtrl', function(){ 
 
    //Phone list controller tests goes here 
 
    } 
 

 
    describe('PhoneDetailsCtrl', function(){ 
 
    //Phone details controller tests goes here 
 
    } 
 
}

它增加了什么,但在测试中分离案例。 Click here有关描述方法的更多信息

+0

那么描述方法可以有一个原始名称?它不必对应于一个已定义的控制器? – mdarmanin

+1

@emporio - 它的随机文本。它只是对测试套件的有意义的描述! –

+0

非常感谢您的专业知识! – mdarmanin