我和我的團隊一直在使用茉莉花的Karma測試跑步者設置控制器測試時遇到問題。在Karma測試角度控制器
應用程序/ app.js
buyItApp = angular.module('buyItApp', ['ionic']);
應用程序/控制器/ userSearchCtrl.js
buyItApp.controller("userSearchCtrl", function($http, $scope) {
var self = this;
self.searchQuery = function(searchData) {
$("ion-spinner").toggle(true);
$http({
method: 'GET',
url: 'http://productfeedtest.wandoso.com/?keyword=' + searchData
}).then(function successCallback(response) {
$("ion-spinner").toggle(false);
$scope.showResultsCount = true
$scope.items = response.data.items;
$scope.isOffer = response.data.is_special_offer;
$scope.resultsCount = response.data.items.length;
$scope.searchQuery = searchData;
}, function errorCallback(response) {
$scope.searchResults = "server error!"
});
}
});
測試/ karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular-mocks.js',
'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular-scenario.js',
'../public/javascripts/app/*.js',
'../public/javascripts/app/controllers/*.js',
'unit/userSearchCtrl.spec.js',
'unit/*.spec.js'
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: false,
concurrency: Infinity
})
}
測試/單元/ userSearchCtrl.spec.js
describe('userSearchCtrl', function() {
var $rootScope, $scope, $controller, userSearchCtrl;
beforeEach(module('buyItApp'));
beforeEach(inject(function(_$rootScope_, _$controller_){
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$controller = _$controller_;
userSearchCtrl = $controller('userSearchCtrl', {'$rootScope' : $rootScope, '$scope': $scope});
}));
it('should exist', function() {
expect(userSearchCtrl).toBeDefined();
});
});
的失敗的測試結果
最值得注意的是:
Expected undefined to be defined.
at /Users/Harry/Dropbox/Dev/buyIt/test/unit/userSearchCtrl.spec.js:16
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.004 secs/0.007 secs)
它看起來像噶找不到控制器,但是檢查代碼,並審查其他項目也不是那麼清楚是什麼問題了。
謝謝,這解決了這個問題(從我的應用程序中刪除離子通過測試) - 如果你有任何想要在userSearchCtrl.spec.js中注入Ionic的想法,我將不勝感激,如果你能提供一個提示,我是新手測試角度。 – Harry
@Stussy如果你不需要控制器中的'離子'依賴,那麼常見的方法是讓'buyItApp.controllers'模塊單獨測試它,所以'buyItApp'加載['buyItApp.controllers','ionic']。 – estus