私は自分のアプリケーションの単体テストを作成しようとしています。私の主な目標は、コンポーネントまたはサービスの基本スペックファイルを作成して、そのコンポーネントが依存するすべてのサービスまたはコンポーネントがインポートされているかどうかを確認することです(これは私が考える最も基本的な仕様ファイルです)。私はインターネット上で検索しようとしたが有用な何かを考え出すことができませんでした。 ご協力いただければ幸いです。依存ユニットテスト:角2
0
A
答えて
0
Angular-Cliをご覧ください。 Angular 2アプリケーションの作成に役立ちます。コンポーネントを生成すると、ng g component my-new-component
は、そのコンポーネントのファイルを自動的に作成します(.spec
)。 Angular-Cliはまた、のテスト環境を指定してHEREと設定します。
これはどれくらい助けているのかわかりませんが、あなたを助けるには十分かもしれません。
だから私はNAV-バーコンポーネント
何とか-NAV-bar.component.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-blah-nav-bar',
templateUrl: './app/blah-nav-bar/blah-nav-bar.component.html',
styleUrls: ['./app/blah-nav-bar/blah-nav-bar.component.css']
})
export class BlahNavBarComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
}
}
そして、生成されたspecファイルはこの
何とか-NAVのように見えてい-bar.component.spec.ts
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { async, inject } from '@angular/core/testing';
import { BlahNavBarComponent } from './blah-nav-bar.component';
describe('Component: BlahNavBar',() => {
it('should create an instance',() => {
let component = new BlahNavBarComponent();
expect(component).toBeTruthy();
});
});
私はspecファイルをコメントアウトので、私はこれについてはあまり知らないが、多分私が公式にこれを見つけたあなたは
UPDATE
を始めるために何かを刺激するのに十分です角度2のサイトも同様です。ここにAngular 2 Testingホームページへのリンクがあります。それは説明しますTesting a Component with a Dependency
したがって、コンポーネントの注入サービスをテストする場合。
アプリ/ .spec
この
アプリ/ welcome.component.spec.ts
import { WelcomeComponent } from './welcome.component';
import { UserService } from './model';
beforeEach(() => {
// stub UserService for test purposes
userServiceStub = {
isLoggedIn: true,
user: {
name: 'Test User'
}
};
TestBed.configureTestingModule({
declarations: [WelcomeComponent],
providers: [{
provide: UserService,
useValue: userServiceStub
}]
});
fixture = TestBed.createComponent(WelcomeComponent);
comp = fixture.componentInstance;
// UserService from the root injector
userService = TestBed.get(UserService);
// get the "welcome" element by CSS selector (e.g., by class name)
de = fixture.debugElement.query(By.css('.welcome'));
el = de.nativeElement;
});
// The Tests
it('should welcome the user',() => {
fixture.detectChanges();
const content = el.textContent;
expect(content).toContain('Welcome', '"Welcome ..."');
expect(content).toContain('Test User', 'expected name');
});
it('should welcome "Bubba"',() => {
userService.user.name = 'Bubba'; // welcome message hasn't been shown yet
fixture.detectChanges();
expect(el.textContent).toContain('Bubba');
});
it('should request login if not logged in',() => {
userService.isLoggedIn = false; // welcome message hasn't been shown yet
fixture.detectChanges();
const content = el.textContent;
expect(content).not.toContain('Welcome', 'not welcomed');
expect(content).toMatch(/log in/i, '"log in"');
});
ようになり
import { Component, OnInit } from '@angular/core';
import { UserService } from './model';
@Component({
selector: 'app-welcome',
template: '<h3 class="welcome"><i>{{welcome}}</i></h3>'
})
export class WelcomeComponent implements OnInit {
welcome = '-- not initialized yet --';
constructor(private userService: UserService) { }
ngOnInit(): void {
this.welcome = this.userService.isLoggedIn ?
'Welcome, ' + this.userService.user.name :
'Please log in.';
}
}
welcome.component.ts
関連する問題
- 1. コード依存ユニットテスト
- 2. 角2依存性注入
- 3. 依存性注入角2 - イオン2&タイスクリプト
- 4. 角2ユニットテスト
- 5. 角2 JWTユニットテスト
- 6. 角2ユニットテスト:@Inject( 'Window')をオーバーライドする方法private _window:ウィンドウコンストラクタの依存関係?
- 7. デコレータの角2依存性注入
- 8. 角2のテストの依存関係
- 9. 依存性注入問題 - 角度2
- 10. 角度2を注入依存コンストラクタ
- 11. 円の依存性注入角度2
- 12. 角2 - 依存性注入とバレルリング
- 13. ユニットテストのJMS依存関係
- 14. データアクセス、ユニットテスト、依存性注入
- 15. ユニットテストの依存関係プロパティ
- 16. 角度2 - ルータでのユニットテスト
- 17. 角度2ユニットテスト - 複数クラスインスタンス
- 18. 依存性を持つ角2角パイプ試験
- 19. 角度2アプリケーション:角2スタイルガイド上に円形モジュールの依存関係
- 20. ユニットテストの依存関係について
- 21. angle2、依存性注入のユニットテスト
- 22. サービス(依存関係あり)angular2のユニットテスト
- 23. ユニットテストにおける依存性注入
- 24. ネットワーク依存コードによるユニットテスト
- 25. ユニットテスト - インタフェース、モッキングと依存性の注入
- 26. 角度2 ngModelによるユニットテスト
- 27. 角度2とサービスによるユニットテスト
- 28. 角度2ユニットテストでHttpを使用
- 29. 角2 Transclusion/ng-content、依存関係の通信
- 30. angular2-cliプロジェクト内で角度2の依存関係をアップグレード