在jasmine中调用$scope方法

Calling $scope methods in jasmine

本文关键字:scope 方法 调用 jasmine      更新时间:2023-09-26

我对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中的诞生地。任何帮助,即使它只是一个链接到一个更好的教程,我们都将感激不尽。

您不能在"beforeEach"answers"it"函数之外的测试描述中编写测试代码。

:

  $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");
  });

需要看起来像:

describe('calling changeToJackie',function () {
    beforeEach(function() {
        $scope.changeToJackie();
    });
    it('should now say Jackie', function() {
        expect($scope.name).toEqual("Jackie");
    });
    it ('should say George if I call changeToGeorge', function() {
        $scope.changeToGeorge();
        expect($scope.name).toEqual("George");
    }); 
});