2013-04-08 32 views
0

這很可能是我不知道如何使用Karma。說我有一個angularJS樣板測試,像這樣

 
'use strict'; 

describe('Controller: MainCtrl', function() { 

    // load the controller's module 
    beforeEach(module('testingApp')); 

    var MainCtrl, 
    scope; 

    // Initialize the controller and a mock scope 
    beforeEach(inject(function ($controller) { 
    scope = {}; 
    MainCtrl = $controller('MainCtrl', { 
     $scope: scope 
    }); 
    })); 

    it('should attach a list of awesomeThings to the scope', function() { 
    expect(scope.awesomeThings.length).toBe(3); 
    }); 
}); 

如果我插入一個$在我的控制器發出像這樣

 
'use strict'; 

angular.module('testingApp') 
    .controller('MainCtrl', function ($scope) { 
    $scope.awesomeThings = [ 
     'HTML5 Boilerplate', 
     'AngularJS', 
     'Karma' 
    ]; 

    $scope.$emit('helloWorld'); 
    }); 

我現在測試失敗,並說

TypeError: Object # has no method '$emit'
任何幫助,非常感謝!

回答

1

而是提供空的對象範圍,使用方法:

$scope = $rootScope.$new() 

所以,你的代碼是:

// Initialize the controller and a mock scope 
    beforeEach(inject(function ($controller) { 
    scope = $rootScope.$new(); 
    MainCtrl = $controller('MainCtrl', { 
     $scope: scope 
    }); 
    })); 
+0

謝謝valentyn!這個伎倆 – climboid 2013-04-15 13:57:50