I understand that in Unit Test, hard-coded is preferred as we don't want to write much code as possible.
For Integration Testing however, is the same principle still applied? For a context, here's a common scenario:
- TodoController
- TodoService
- Serializer/Transformer/Whatever you call it
Here's an imaginary code on how both approach might look like:
- Hard Coded
// Arrange
$note = new Note('note123', 'John', 'example message');
$this->noteRepository->save($note);
// Act
$response = $this->json('GET', '/api/notes');
// Assert
$response->seeJsonEquals([
'id' => 'note123',
'author' => 'John',
'message' => 'example message'
]);
- Soft Coded (Computed)
// Arrange
$note = new Note('note123', 'John', 'example message');
$this->noteRepository->save($note);
// Act
$response = $this->json('GET', '/api/notes');
// Assert
$noteSerializer = new NoteSerializer();
$response->seeJsonEquals($noteSerializer->serialize($note));
The only catch for the soft-coded approach is that if the Serializer is the problem, the test will still pass because both the controller and expected value uses it.
However, we can solve that by creating another test for the Serializer.
We might have lots of mini-tests but I think it'll us save lots of time compared to hard-coding. If we changed our response structure, the hard-coded tests will need some changes as well but the soft-coded one needs to change in its own test only.
Aucun commentaire:
Enregistrer un commentaire