2015-06-30 52 views
1

在文件dashboard.module.coffee我有以下聲明:因果報應找不到angularjs指令在它自己的文件

angular 
    .module('app.dashboard', []) 

在另一個文件中,stat.directive.coffee我有以下幾點:

angular.module('app.dashboard') 
    .directive('stat', ['$interval', statDirective]) 

statDirective包含指令邏輯。此代碼工作正常在瀏覽器中,即<stat>元素按預期工作,但下面的茉莉花測試不渲染元素,它只是一個空字符串:

describe "Stat", -> 
    element = null 
    scope = null 

    beforeEach module 'app.dashboard' 
    beforeEach module 'views/templates/dashboard/stat.html' 
    beforeEach inject ($compile, $rootScope) -> 
    scope = $rootScope.$new() 
    element = $compile('<stat></stat>') scope 

    it "contains some html", -> 
    scope.$digest() 

    expect(element.html()).toEqual('<div>hi</div>') 

我已經縮小下來的模塊是與指令分開申報。相反,如果該聲明是這樣,該指令被發現,呈現:

angular.module('app.dashboard', []) 
    .directive('stat', ['$interval', statDirective]) 

在這裏,唯一的變化是,模塊和指令被宣告在一起,而不是兩個文件。

由於代碼在瀏覽器中工作得很好,這看起來與Karma特別相關。有沒有在我的Karma配置中缺少這種類型的文件結構的工作?

這裏是我的噶配置:

// Karma configuration 
// Generated on Mon Jun 29 2015 22:33:24 GMT-0700 (PDT) 

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 
     'client/bower_components/jquery/dist/jquery.min.js', 

     {pattern: 'client/bower_components/**/*.map', watched: false, included: false, served: true}, 

     'client/bower_components/angular/angular.min.js', 
     'client/bower_components/angular-websocket/angular-websocket.js', 

     'node_modules/angular-mocks/angular-mocks.js', 

     'client/views/**/*.html', 

     'client/scripts/*.module.js', 
     'client/scripts/**/*.module.js', 
     'client/scripts/**/*.module.coffee', 
     'client/scripts/**/*.directive.coffee', 
     'client/scripts/**/*.controller.coffee', 
     'client/scripts/**/*.coffee', 

     'spec/**/*Spec.coffee' 
    ], 


    // list of files to exclude 
    exclude: [ 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
     '**/*.coffee': ['coffee'], 
     'client/views/**/*.html': 'ng-html2js' 
    }, 

    ngHtml2JsPreprocessor: { 
     // strip app from the file path 
     stripPrefix: 'client/' 
    }, 

    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 


    // web server port 
    port: 9876, 


    // enable/disable colors in the output (reporters and logs) 
    colors: true, 


    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['Chrome'], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false 
    }) 
}; 

謝謝!

+0

發表您的karma.config文件YOH 。 – SoEzPz

+0

@SoEzPz編輯了包含karma配置的問題,謝謝! – berto

回答

1

也許我們不得不採取這個步驟...

我看你的指令統計具有依賴性:$間隔。

如果我記得,您可能需要包含此服務才能使其正常運行。

你可以試試嗎?

beforeEach inject ($compile, $rootScope, _$interval_) -> 
    scope = $rootScope.$new() 
    element = $compile('<stat></stat>') scope 
    $interval = _$interval_; // you may not need this line, just the inject. 

UPDATE

好吧,試試這個以及

更改此

element = $compile('<stat></stat>') scope 

對此

element = $compile(angular.element('<stat></stat>'))(scope); 
+0

我仍然得到相同的錯誤。順便說一下,'_ $ interval_'周圍應該只有一個下劃線。我得到一個完全不同的錯誤,直到我刪除了額外的下劃線。謝謝! – berto

+0

你是對的,抱歉太多了。 – SoEzPz

+0

這個更新是否工作? – SoEzPz

相關問題