2016-11-16 55 views
0

我遵循AngularJS:Up and Running一書,在作者說使用Karma和Jasmine進行測試的章節中,他沒有說太多的話來說明如何組織你的項目和準確安裝Karma和Jasmine的地方。我無法在我的項目上運行Karma和Jasmine

我安裝了nodejs並運行良好。然後我把xampp/htdocs/angularjs-up-and-running我的項目與要測試的文件。

在同一個文件夾我有none_modules。在這最後一個文件夾中,我有業力,業力 - 茉莉花和業 - 鉻 - 發射器。

從控制檯我在c去:/ XAMPP/htdocs中/ angularjs向上和運行/ none_modules /人緣文件夾,我用命令:

karma init 

我回答了所有問題,然後我然後使用:

karma start 

Chrome瀏覽器打開這樣的: Chrome Debug

,但我不知道如何來測試我的js文件。我試圖把http://localhost:9876/controller.js來測試我的文件,但我得到這個在我的控制檯:

Console Image

Controller.js

angular.module('notesApp', []).controller('ListCtrl', [ function(){ 
var self = this; 
self.items = [ 
    {id: 1, label: 'First', done: true}, 
    {id: 2, label: 'Second', done: false} 
]; 

self.getDoneClass = function(item) { 
    return { 
     finished: item.done, 
     unfinished: !item.done 
    }; 
}; 

}]);

我是新來的angularjs和這個測試之王。我在interned上搜索瞭解決方案,但是我的問題是我不知道如何使用我的文件controller.js來測試它,並且我沒有找到解決方案。 請有人在這種情況下伸出援手。

+0

你需要編寫規範文件和噶配置指定它們。無需在Chrome窗口中執行任何操作。 – estus

+0

你的意思是在這裏改變:'module.exports = function(config){ config.set({0121}}}}} angularjs向上和運行」, //框架使用 //可用框架:https://npmjs.org/browse/keyword/karma-adapter 框架:[ '茉莉'], //在瀏覽器中加載的文件/模式列表 文件:[controller。JS ],文件 //列表排除 排除:[ ], ' – dragon

+0

是,包括規範文件存在,像http://karma-runner.github.io/1.0/config/files .html – estus

回答

1

有一個文件叫做karma.conf.js。在這個文件中,你可以用包含你想運行的測試的文件數組來指定'files'參數。所以你會寫一個名爲'app/mytest.js'的測試,並且在你的karma.conf.js文件中,你將把路徑放到那個測試中。

module.exports = function(config) { 
config.set({ 
    files: [ 
     "app/test.js", 
    ], 
}); 
}; 

請注意,您需要在測試文件本身中包含角度模塊和控制器依賴關係。

應用程序的內容,所以/ mytest.js可能看上去像(從angular documentation:

describe('PasswordController', function() { 
    beforeEach(module('app')); 

    var $controller; 

    beforeEach(inject(function(_$controller_){ 
    // The injector unwraps the underscores (_) from around the parameter names when matching 
    $controller = _$controller_; 
    })); 

    describe('$scope.grade', function() { 
    it('sets the strength to "strong" if the password length is >8 chars', function() { 
     var $scope = {}; 
     var controller = $controller('PasswordController', { $scope: $scope }); 
     $scope.password = 'longerthaneightchars'; 
     $scope.grade(); 
     expect($scope.strength).toEqual('strong'); 
    }); 
    }); 
}); 
+0

我得到這個錯誤後,我做了你說的:ReferenceError:test is not defined – dragon

+0

這不是一個一步一步的解決方案,而是一個設置你的測試的指導方針。如果你想要一個確切的答案,發佈一個鏈接到一個要點或你的github回購。 –

+0

雖然如果你使用Karma和Jasmine,你也會陷入這些錯誤。我解決了這個錯誤,現在我得到'Uncaught ReferenceError:angular is not defined'。感謝您試圖幫助我。 – dragon

相關問題