有沒有人有如何單元測試提供者的例子?如何測試AngularJS自定義提供者
例如:
config.js
angular.module('app.config', [])
.provider('config', function() {
var config = {
mode: 'distributed',
api: 'path/to/api'
};
this.mode = function (type) {
if (type) {
config.isDistributedInstance = type === config.mode;
config.isLocalInstance = !config.isDistributedInstance;
config.mode = type;
return this;
} else {
return config.mode;
}
};
this.$get = function() {
return config;
};
}]);
app.js
angular.module('app', ['app.config'])
.config(['configProvider', function (configProvider) {
configProvider.mode('local');
}]);
app.js
是使用測試和我看到已經配置configProvider
和我可以測試它作爲一項服務。但是我怎樣才能測試配置的能力?或者它根本不需要?
感謝您對這個職位!我按照這裏找到的指南,並失敗:https://github.com/angular/angular.js/issues/2274。 上述示例按預期工作。謝謝! – zayquan 2014-10-26 23:42:35
當這樣做時,我不得不改變假模塊的聲明來傳遞一個空數組而不是一個空函數。可能是由於更新版本的Angular。 – 2016-10-13 14:44:50