2016-07-27 51 views
1

karma.config.js:控制器在茉莉測試未定義

module.exports = function(config) { 
    config.set({ 
    basePath: '', 
    frameworks: ['jasmine'], 
    files: [ 
     'node_modules/angular/angular.min.js', 
     'node_modules/angular-mocks/angular-mocks.js', 
     'node_modules/angular-translate/dist/angular-translate.min.js', 
     'browser/javascripts/*.js', 
     'browser/tests/*.spec.js' 
    ], 
    exclude: [], 
    preprocessors: {}, 
    reporters: ['progress'], 
    port: 9876, 
    colors: true, 
    logLevel: config.LOG_INFO, 
    autoWatch: true, 
    browsers: ['Chrome'], 
    singleRun: false, 
    concurrency: Infinity 
    }) 
}; 

home.spec.js:

describe('Home Controller', function() { 
    beforeEach(
    module('pascalprecht.translate'), 
    module('tradeshiftApp') 
); 
    var $controller; 
    beforeEach(inject(function(_$controller_){ 
    $controller = _$controller_; 
    })); 

    it('should exist', function(){ 
    controller = $controller('HomeController', { 
     $scope: {} 
    }); 
    expect(controller).not.toBe(undefined); 
    }) 
}); 

我使用karma-jasmine和問題如下:我app.js文件有這個模塊,它的加載正確:

var app = angular.module('tradeshiftApp', ['pascalprecht.translate']); 

但是當我嘗試嘲笑我的控制器,這是

app.controller('HomeController', function ($scope, $req, $window, $translate, $q) { 
    // some code 
}); 

我得到一個錯誤,它說,HomeController不是一個函數。正如你所看到的,依賴關係是有線的,並且所有應該都好,我想。有小費嗎?

注:我試圖注入$rootScope並得到$rootScope.$new(),它很成功。

回答

0

那麼,問題是'pascalprecht.translate'模塊。在我的代碼中,我有一個小的依賴關係連接到上述模塊,它包含在我的HTML文件中,但不包含在我的Karma配置文件中。夥計們,小心和警惕你的代碼:)

1

試試這個:

beforeEach(function() { 
 
     angular.module('pascalprecht.translate', []) 
 
     angular.mock.module('tradeshiftApp') 
 
    } 
 
);

+0

它會拋出一個錯誤'TypeError:queueableFn.fn.call不是一個函數' –

相關問題