プロバイダの単体テスト方法の例はありますか?例えば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
これを行うとき、空の関数の代わりに空の配列に渡すように偽のモジュールの宣言を変更しなければなりませんでした。 Angularの新しいバージョンが原因である可能性があります。 –