I am a bit confused about this test:
describe('#getUsers', () => {
it('should return an Observable<User[]>', () => {
const dummyUsers: User[] = [
new User(0, 'John'),
new User(1, 'Doe')
];
service.getUsers().subscribe(users => {
expect(users.length).toBe(2);
expect(users).toEqual(dummyUsers);
});
const req = httpMock.expectOne(`${service.API_URL}/users`);
expect(req.request.method).toBe('GET');
req.flush(dummyUsers);
});
});
I saw the similar examples many times, during learning about tests in the Angular applications.
If I see good, we are declaring an array of Users and then we are returning the same array in response of request.
Finally we are checking if the created array is the same as received. I can't understand the purpose, it looks really strange to me.
What is the point of comparing the same array to the same array?
Shouldn't I make a real GET to the API and then check if there are elements in response?
You are not testing the data, you are testing the method. You provide some data and let the method get that data. If the method functions correctly, you should get the same data as provided.
But I guess your problem is more in understanding unit tests. The integration part of the test comes after that, where the request type and number of calls are checked.
I guess it is difficult to dertermine where you make the cut for an integration test. You could test against the real API but you also increase the dependencies of the test then and that makes them harder to maintain.
I'd argue that checking if the API returns values at all is more easily tested in a unit test.