在茉莉花中调用$ scope方法

时间:2013-06-13 19:48:05

标签: javascript angularjs jasmine karma-runner

我对棱角分明和茉莉是非常新的,所以如果这是一个基本问题,我很抱歉,我忽略了我读过的文档和教程中的内容。我正在为一个新的角度项目创建我的第一个单元测试。我遇到了调用方法和一些变量的麻烦。

我正在测试这个虚拟控制器:

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

我在这个更基本的版本中遇到了更多的错误,我发现了我的实际单元测试(特别是调用$ scope.method(“param”)来调用$ http.PUT。我的错误看起来更多喜欢:

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)

请原谅我的角度/ jasmine / javascript的诞生。任何帮助都将不胜感激,即使它只是一个更好的教程的链接。

1 个答案:

答案 0 :(得分:3)

您无法在“beforeEach”和“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");
    }); 
});