でテストするために1つの値以上のものを提供しています。しかし、私はPlaceholderSupportService()
の値をfalse
にしました。私はtrue
とこの値を持っている2番目のテストを実行したい。 it
の文の中で$provide
にアクセスできないようですが、どうすればいいですか?
1
A
答えて
3
あなたはPlaceholderSupportService
に変数を返すよう指示できます。そして、テストブロックで、変数の値を変更します。これは問題を解決するはずです。ここにその例があります:
describe('placeholder directive', function() {
// Bindable members
var element;
var providerResult = false; //Here is the variable that you will change.
// Load module
beforeEach(angular.mock.module('app'));
// Mock service response
beforeEach(module(function($provide) {
$provide.value('PlaceholderSupportService', function() {
return providerResult;
});
}));
// Check the correct HTML is rendered
it('Renders placeholder as input value when placeholder is not supported', inject(function($timeout) {
element = $compile('<input name="test" placeholder="Test" />')($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(element[0].value).toBe('Test');
}));
// Check the correct HTML is rendered
it('Renders placeholder as input value when placeholder is supported', inject(function($timeout) {
providerResult = true; //Here I will change the result of my PlaceholderSupportService
element = $compile('<input name="test" placeholder="Test" />')($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(element[0].value).toBe('Test');
}));
});
0
it
ごとにbeforeEach
を与えるために、テストを簡単に再構成できます。このコードはおそらくちょっと整理できますが、ここにその考えがあります。
describe('placeholder directive', function() {
// Bindable members
var element;
describe('when placeholder is supported', function() {
// Load module
beforeEach(angular.mock.module('app'));
// Mock service response
beforeEach(module(function($provide) {
$provide.value('PlaceholderSupportService', function() {
return true;
});
}));
// Bind references to global variables
beforeEach(inject(function($compile, $rootScope) {
element = $compile('<input name="test" placeholder="Test" />')($rootScope);
$rootScope.$digest();
}));
// Check the correct HTML is rendered
it('Renders placeholder as input value when placeholder is not supported', inject(function($timeout) {
$timeout.flush();
expect(element[0].value).toBe('Test');
}));
});
describe('when placeholder is not supported', function() {
// Load module
beforeEach(angular.mock.module('app'));
// Mock service response
beforeEach(module(function($provide) {
$provide.value('PlaceholderSupportService', function() {
return false;
});
}));
// Bind references to global variables
beforeEach(inject(function($compile, $rootScope) {
element = $compile('<input name="test" placeholder="Test" />')($rootScope);
$rootScope.$digest();
}));
// Check the correct HTML is rendered
it('Renders placeholder as input value when placeholder is not supported', inject(function($timeout) {
$timeout.flush();
expect(element[0].value).toBe('Test');
}));
});
});
関連する問題
- 1. は、私は次のディレクティブを持って角度
- 2. 角度 - 私はディレクティブのテンプレートでフォームを持っている子ディレクティブ
- 3. 角度カルマジャスミンforEach @エラー
- 4. カルマジャスミン角速度:[$ injector:nomod]
- 5. カルマジャスミン非同期テストは
- 6. Javascript角張った工場説明
- 7. 角度2.0のデモチュートリアル - 説明
- 8. 角2カスタムパイプの説明
- 9. Python unittestフレームワーク:テストの説明
- 10. Rubyは私が説明してインスタンスを持っている
- 11. テスト角度/ジャスミンディレクティブは、私がtemplateUrlが含まれている角度ディレクティブの以下のカルマ/ジャスミンのテストを持っているテンプレートHTML
- 12. カルマジャスミンによる角度テストが失敗しました
- 13. 角度ラッパーコンポーネントは、私は次のようになりますディレクティブ持っている追加のディレクティブ
- 14. MiniTestと非ラテンのテストの説明
- 15. Object.assign()奇妙な行動は、私はこのコードを持って説明
- 16. JUnitレポート - テスト方法の説明
- 17. SpringデータNeo4j Neo.ClientError.Statement.EntityNotFound;説明:私は、次のNeo4jスキーマを持っているID
- 18. Javaの整数MIN_VALUEの負その後、私は明日のテストを持っていると私は私の本の説明を理解カント
- 19. は、私は、次の要素で構成ディレクティブを持って毎回ディレクティブは
- 20. hex2chars&Iは、関数hex2charsを持って
- 21. iが3ページ持って
- 22. ^require-access親スコープを持つ角ディレクティブ?
- 23. テスト用のディレクティブにデータを渡すときの角型ディレクティブユニットのテストの問題
- 24. は、私はこのような画像や説明でいっぱいのリストを持っているホバーイベント
- 25. 私が持っている...私が説明させて、(以前のディレクトリ参照)
- 26. ヘッダーは説明できない高さを持っています
- 27. 角度材料Iは、角材料datapickerとの基本的な例を持っている
- 28. カーネルドライバのI/Oインタフェース機能の説明を探しています
- 29. ダイアグラマ/ Iは、Rマークダウンファイル持って
- 30. クローズ、ディスポジション、ファイナライズ、GC、Idisposable、....あなたはそれらの明確な説明を持っていますか?
私はそうすることができると考えました。しかし確かに、1行の単語を変更するために23行のコードを複製せずにこれを行う方法がありますか? – Coop