@@ -5,16 +5,7 @@ import {
5
5
inject ,
6
6
TestBed ,
7
7
} from '@angular/core/testing' ;
8
- import {
9
- BaseRequestOptions ,
10
- ConnectionBackend ,
11
- Http ,
12
- HttpModule ,
13
- Response ,
14
- ResponseOptions ,
15
- } from '@angular/http' ;
16
- import { MockBackend } from '@angular/http/testing' ;
17
- < % _ if ( filters . mocha && filters . expect ) { - % >
8
+ import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ; < % if ( filters . mocha && filters . expect ) { % >
18
9
import { expect } from 'chai' ; < % } % > < % if ( filters . uibootstrap ) { % >
19
10
import { TooltipModule } from 'ngx-bootstrap' ; < % } % >
20
11
import { FormsModule } from '@angular/forms' ; < % if ( filters . ws ) { % >
@@ -25,51 +16,48 @@ import { MainComponent } from './main.component';
25
16
describe ( 'Component: MainComponent' , function ( ) {
26
17
let comp : MainComponent ;
27
18
let fixture : ComponentFixture < MainComponent > ;
19
+ let httpTestingController : HttpTestingController ;
20
+ const mockThings = [ 'HTML5 Boilerplate' , 'AngularJS' , 'Karma' , 'Express' ] ;
28
21
29
22
beforeEach ( async ( ( ) => {
30
23
TestBed . configureTestingModule ( {
31
24
imports : [
32
25
FormsModule , < % if ( filters . uibootstrap ) { % >
33
26
TooltipModule . forRoot ( ) , < % } % >
34
- HttpModule ,
27
+ HttpClientTestingModule ,
35
28
],
36
- declarations: [ MainComponent ], // declare the test component
29
+ declarations: [ MainComponent ], // declare the test component< % if ( filters . ws ) { % >
37
30
providers: [
38
- BaseRequestOptions,
39
- MockBackend,
40
- {
41
- provide : Http ,
42
- useFactory : ( backend : ConnectionBackend , defaultOptions : BaseRequestOptions ) => {
43
- return new Http ( backend , defaultOptions ) ;
44
- } ,
45
- deps : [ MockBackend , BaseRequestOptions ]
46
- } ,< % if ( filters . ws ) { % >
47
- { provide : SocketService , useClass : SocketServiceStub } ,< % } % >
48
- ],
31
+ { provide : SocketService , useClass : SocketServiceStub } ,
32
+ ],< % } % >
49
33
} ) . compileComponents ( ) ;
50
- } ) ) ;
51
34
52
- beforeEach ( async ( inject ( [ MockBackend ] , ( mockBackend ) => {
53
- mockBackend . connections . subscribe ( conn => {
54
- conn . mockRespond ( new Response ( new ResponseOptions ( {
55
- body : JSON . stringify ( [ 'HTML5 Boilerplate' , 'AngularJS' , 'Karma' , 'Express' ] )
56
- } ) ) ) ;
57
- } ) ;
58
- } )));
35
+ httpTestingController = TestBed . get ( HttpTestingController ) ;
36
+ } ) ) ;
59
37
60
38
beforeEach ( async ( ( ) => {
61
39
fixture = TestBed . createComponent ( MainComponent ) ;
62
40
// MainComponent test instance
63
41
comp = fixture . componentInstance ;
64
42
65
43
/**
66
- * Trigger initial data binding.
44
+ * Trigger initial data binding and run lifecycle hooks
67
45
*/
68
46
fixture . detectChanges ( ) ;
69
47
} ));
70
48
71
- it('should attach a list of things to the controller', () => { < % if ( filters . jasmine ) { % >
72
- expect ( comp . awesomeThings . length ) . toEqual ( 4 ) ; < % } else if(filters.mocha) { % >
73
- < %= expect ( ) % > comp . awesomeThings . length < %= to ( ) % > . equal ( 4 ) ; < % } %>
49
+ it('should attach a list of things to the controller', () => {
50
+ // `GET /api/things` should be made once
51
+ const req = httpTestingController . expectOne ( '/api/things' ) ;
52
+ expect ( req . request . method ) . to . equal ( 'GET' ) ;
53
+
54
+ // Respond with mock data
55
+ req . flush ( mockThings ) ;
56
+
57
+ // assert that there are no outstanding requests
58
+ httpTestingController . verify ( ) ;
59
+
60
+ < % if ( filters . jasmine ) { % > expect ( comp . awesomeThings ) . toEqual ( mockThings ) ; < % } else if(filters.mocha) { % >
61
+ < %= expect ( ) % > comp . awesomeThings < %= to ( ) % > . equal ( mockThings ) ; < % } %>
74
62
} ) ;
75
63
} ) ;
0 commit comments