Skip to content

Instantly share code, notes, and snippets.

View stevermeister's full-sized avatar
🇺🇦

Stepan Suvorov stevermeister

🇺🇦
View GitHub Profile
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');
service.getOne(1).subscribe((user) => {
expect(user).toEqual(mockUser);
});
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));
})
}));
it('should get one user', inject([XHRBackend], (mockBackend: MockBackend) => {
mockBackend.connections.subscribe((connection: MockConnection) => {
//...
})
}));
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [{provide: XHRBackend, useClass: MockBackend}]
});
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) { }
import { Injectable } from '@angular/core';
@Injectable()
export class CalcService {
sum(a, b) {
return a + b;
}
sumAsync(a, b) {
it('should throw an error', () => {
const pipe = new SexPipe();
expect( function(){ pipe.transform(2); } ).toThrow(new Error('Input should be only 0 or 1'));
});
import { SexPipe } from './sex.pipe';
describe('SexPipe', () => {
it('create an instance', () => {
const pipe = new SexPipe();
expect(pipe).toBeTruthy();
});
it('check pipe output', () => {
const pipe = new SexPipe();
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sex'
})
export class SexPipe implements PipeTransform {
transform(value: number): string {
if (value === 0) {
return 'ж'