Skip to content

Instantly share code, notes, and snippets.

@sourcerebels
Created September 24, 2017 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sourcerebels/d187b7899409173f34775a61805d1854 to your computer and use it in GitHub Desktop.
Save sourcerebels/d187b7899409173f34775a61805d1854 to your computer and use it in GitHub Desktop.
ts-mokito-sample.ts
import { mock, instance, when, verify, anyString, anyOfClass } from 'ts-mockito';
import { RequestOptions } from '@angular/http';
import { HttpService } from '../common/http/http.service';
import { LoginService } from './login.service';
import { RouterTestingModule } from '@angular/router/testing';
import { Observable } from 'rxjs';
describe('login.service', () => {
let httpService : HttpService;
let loginService : LoginService;
beforeEach(() => {
console.log('beforeEach');
httpService = mock(HttpService);
loginService = new LoginService(instance(httpService));
});
it('executes a POST request to the backend on user login', () => {
loginService.login('username', 'password');
verify(httpService.executeRequest(anyString(), anyOfClass(RequestOptions))).called();
});
});
@sourcerebels
Copy link
Author

Una diferencia respecto a la versión de Java es que hay usar "instance" a la hora de proporcionar el mock al objeto que se está testeando.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment