Mudanças entre as edições de "Unit testing with default Angular installation"
De Basef
Linha 5: | Linha 5: | ||
<source lang="javascript"> | <source lang="javascript"> | ||
let mockedLoginService = TestBed.get(LoginService); | let mockedLoginService = TestBed.get(LoginService); | ||
+ | </source> | ||
+ | |||
+ | == Asserting that a value is equal to something == | ||
+ | |||
+ | <source lang="javascript"> | ||
+ | |||
+ | expect(someVariable).toEqual('something'); | ||
+ | |||
+ | </source> | ||
+ | |||
+ | == Asserting that a value is defined == | ||
+ | |||
+ | <source lang="javascript"> | ||
+ | |||
+ | expect(someVariable).toBeDefined(); | ||
+ | |||
+ | </source> | ||
+ | |||
+ | == Asserting that a value is true == | ||
+ | |||
+ | <source lang="javascript"> | ||
+ | |||
+ | expect(someVariable).toBeTruthy(); | ||
+ | |||
+ | </source> | ||
+ | |||
+ | == Asserting that a value is false == | ||
+ | |||
+ | <source lang="javascript"> | ||
+ | |||
+ | expect(someVariable).toBeFalsy(); | ||
+ | |||
</source> | </source> | ||
Edição das 14h11min de 26 de abril de 2018
Índice
- 1 Getting instance of a service
- 2 Asserting that a value is equal to something
- 3 Asserting that a value is defined
- 4 Asserting that a value is true
- 5 Asserting that a value is false
- 6 Asserting that some method is called
- 7 Asserting that some method is called with given arguments
- 8 Mocked Http Requests
- 9 Expecting that a method is called X times
Getting instance of a service
let mockedLoginService = TestBed.get(LoginService);
Asserting that a value is equal to something
expect(someVariable).toEqual('something');
Asserting that a value is defined
expect(someVariable).toBeDefined();
Asserting that a value is true
expect(someVariable).toBeTruthy();
Asserting that a value is false
expect(someVariable).toBeFalsy();
Asserting that some method is called
let spySomeMethod = spyOn(someInstance, 'someMethod'); // do something here expect(spySomeMethod).toHaveBeenCalled();
Asserting that some method is called with given arguments
let spySomeMethod = spyOn(someInstance, 'someMethod').and.callFake((arg1, arg2) => { expect(arg1).toEqual(10); expect(arg2).toEqual(20); }); // do something here expect(spySomeMethod).toHaveBeenCalled();
Mocked Http Requests
1) Instead of using HttpClientModule, you should use HttpClientTestingModule. You will also use HttpTestingController. Below how to import and configure them:
... import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing"; ... describe('YourClass', () => { beforeEach(() => { TestBed.configureTestingModule({ imports: ... [ AppRoutingModule, HttpClientTestingModule ] ... }); }); ... });
2) In your test you need to get an instance of HttpTestingController:
... it('should fill user data on successful login', () => { let httpMock = TestBed.get(HttpTestingController); }); ...
3) In the same test you should assert that a call has been made with:
let mockedResponse = { statusCode: 200, body: { anything: 'your endpoint returns' } } const request = httpMock.expectOne('http://some.request.url/to/somewhere'); request.flush(mockedResponse); httpMock.verify();
Expecting that a method is called X times
let spySomeMethod = spyOn(someInstance, 'someMethod'); // do something here expect(spySomeMethod.calls.count()).toEqual(2);