我对 angular 和 jasmine 非常陌生,所以如果这是一个基本问题并且我忽略了我阅读的文档和教程中的某些内容,我很抱歉。我正在为一个新的 Angular 项目创建我的第一个单元测试。我在调用方法和一些变量时遇到了麻烦。
我正在测试这个虚拟控制器:
.controller( 'DummyCtrl', function DummyCtrl($scope){
$scope.name = "TBD";
$scope.changeToJackie = function(){
$scope.name="Jackie";
};
$scope.changeToGeorge = function() {
$scope.name="George";
};
})
使用此单元测试:
describe( 'DummyCtrl', function(){
var $scope, $controller, DummyCtrl;
beforeEach(module('mApp');
beforeEach(inject(function($injector) {
$scope = $injector.get('$rootScope');
$controller = $injector.get('$controller');
DummyCtrl = $controller('DummyCtrl', {$scope: $scope});
it('should start with TBD', function(){
expect($scope.name).toEqual("TBD");
});
$scope.changeToJackie();
it('should now say Jackie', function() {
expect($scope.name).toEqual("Jackie");
});
$scope.changeToGeorge();
it ('should now say George', function() {
expect($scope.name).toEqual("George");
});
});
在我创建的这个更基本的版本中,我得到了更多的错误,以找出我的实际单元测试(特别是调用一个调用 $http.PUT 的 $scope.method("param")。我的错误看起来更像:
Chrome 27.0 (Mac) unauth controllers DummyCtrl should start with TBD FAILED
Error: Argument 'DummyCtrl' is not a function, got undefined
at assertArg (/<path>/build/angular/angular.js:975:11)
at assertArgFn (/<path>/build/angular/angular.js:985:3)
at /<path>/build/angular/angular.js:4656:9
at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:20:16)
at Object.invoke (/<path>/build/angular/angular.js:2820:28)
at workFn (/o<path>/build/angular/angular-mocks.js:1780:20)
Error: Declaration Location
at window.inject.angular.mock.inject (/<path>/build/angular/angular-mocks.js:1766:25)
at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:10:14)
at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:9:2)
at /o<path>/src/app/unauth/unauth.spec.js:1:1
Expected undefined to equal 'TBD'.
Error: Expected undefined to equal 'TBD'.
at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:25:25)
Chrome 27.0 (Mac) unauth controllers DummyCtrl encountered a declaration exception FAILED
TypeError: Cannot read property 'stack' of null
at workFn (/<path>/build/angular/angular-mocks.js:1782:55)
TypeError: Cannot call method 'changeToJackie' of undefined
at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:28:11)
at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:23:3)
at null.<anonymous> (/<path>/src/app/unauth/unauth.spec.js:9:2)
请原谅我在 angular/jasmine/javascript 中的诞生。任何帮助将不胜感激,即使它只是指向更好教程的链接。