This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | |
| import { UserCardComponent } from './user-card.component'; | |
| describe('UserCardComponent', () => { | |
| let component: UserCardComponent; | |
| let fixture: ComponentFixture<UserCardComponent>; | |
| beforeEach(async(() => { | |
| TestBed.configureTestingModule({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { UserService } from '../user.service'; | |
| import { Component } from '@angular/core'; | |
| @Component({ | |
| selector: 'app-user-card', | |
| template: '<div>{{user?.name}}</div>', | |
| providers: [UserService] | |
| }) | |
| export class UserCardComponent { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it('should get one user', inject([XHRBackend, UserService], (mockBackend: MockBackend, service: UserService) => { | |
| mockBackend.connections.subscribe((connection: MockConnection) => { | |
| mockUser = { | |
| name: 'John', | |
| age: 21 | |
| }; | |
| const responseOptions = new ResponseOptions({ body: JSON.stringify(mockUser) }); | |
| connection.mockRespond(new Response(responseOptions)); | |
| expect(connection.request.url).toBe('/users/1'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| service.getOne(1).subscribe((user) => { | |
| expect(user).toEqual(mockUser); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it('should get one user', inject([XHRBackend], (mockBackend: MockBackend) => { | |
| mockBackend.connections.subscribe((connection: MockConnection) => { | |
| mockUser = { name: 'John', age: 21 }; | |
| const responseOptions = new ResponseOptions({ body: JSON.stringify(mockUser) }); | |
| connection.mockRespond(new Response(responseOptions)); | |
| }) | |
| })); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it('should get one user', inject([XHRBackend], (mockBackend: MockBackend) => { | |
| mockBackend.connections.subscribe((connection: MockConnection) => { | |
| //... | |
| }) | |
| })); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| TestBed.configureTestingModule({ | |
| imports: [HttpModule], | |
| providers: [{provide: XHRBackend, useClass: MockBackend}] | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { Http } from '@angular/http'; | |
| import { Observable } from 'rxjs/Observable'; | |
| import 'rxjs/add/operator/map'; | |
| @Injectable() | |
| export class UserService { | |
| constructor(private http: Http) { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| @Injectable() | |
| export class CalcService { | |
| sum(a, b) { | |
| return a + b; | |
| } | |
| sumAsync(a, b) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it('should throw an error', () => { | |
| const pipe = new SexPipe(); | |
| expect( function(){ pipe.transform(2); } ).toThrow(new Error('Input should be only 0 or 1')); | |
| }); |