2016-03-09 92 views
1

我在生成的項目(由yoeman)面臨一些麻煩並且正在測試。無法在業力單元測試中實例化角度控制器

我想要的是構建一個完全自動化的測試環境。我使用Gulp,Karma,Jasmine和角嘲諷。

TypeError: undefined is not an object (evaluating 'controller.awesomeThings') 

在我的控制檯'gulp test'之後它會拋出這個錯誤信息。但這對我來說沒有任何意義。

看看我的測試規範:

'use strict'; 

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

    // load the controller's module 
    beforeEach(function() { 
    module('evoriApp'); 
    }); 

    var controller; 
    var scope; 

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

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

和因果報應應該有它需要的任何文件(karma.conf.js):

files: [ 
     // bower:js 
     'bower_components/angular/angular.js', 
     'bower_components/angular-animate/angular-animate.js', 
     'bower_components/angular-aria/angular-aria.js', 
     'bower_components/angular-cookies/angular-cookies.js', 
     'bower_components/angular-messages/angular-messages.js', 
     'bower_components/angular-resource/angular-resource.js', 
     'bower_components/angular-route/angular-route.js', 
     'bower_components/angular-sanitize/angular-sanitize.js', 
     'bower_components/angular-material/angular-material.js', 
     // endbower 
     'app/scripts/**/*.js', 
     'test/mock/**/*.js', 
     'test/spec/**/*.js' 
    ], 

這是產生一飲而盡方法的樣子:

gulp.task('test', ['start:server:test'], function() { 
    var testToFiles = paths.testRequire.concat(paths.scripts, paths.mocks, paths.test); 
    return gulp.src(testToFiles) 
    .pipe($.karma({ 
     configFile: paths.karma, 
     action: 'watch' 
    })); 
}); 

我不知道失敗在哪裏。我檢查了所有路徑並重寫了多次測試文件...但錯誤沒有改變。

有沒有人有想法,錯誤可能是由什麼引起的?

回答

1

好吧,我明白了。

我試圖在茉莉花獨立環境中運行測試來最小化錯誤根目錄所在的區域。

這是我的控制器是什麼樣子:

angular.module('evoriApp') 
    .controller('AboutCtrl', function ($scope) { 
    $scope.awesomeThings = [ 
     'HTML5 Boilerplate', 
     'AngularJS', 
     'Karma' 
    ]; 
    $scope.testVariable = 2; 
    }); 

我重寫了「this.awesomeThings」到「$ scope.awesomeThings」,並沒有得到它,這是不同的東西。無論...

這是現在正在運行規範:

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

    // load the controller's module 
    beforeEach(function() { 
    module('evoriApp'); 
    console.log("1"); 
    }); 

    var controller; 
    var scope; 

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

    it('sollte instanziert worden sein', function() { 
    expect(controller).toBeDefined(); 
    }); 

    it('sollte ein Object "awesomeThings" besitzen', function() { 
    expect(scope.awesomeThings).toBeDefined(); 
    }); 

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

我的教訓:在一個測試,你必須生成範圍,你傳遞給控制器​​,自己和以後你得爲$ scope.variables使用傳遞的範圍變量。

相關問題