Static utils tests
Last modified on Mon 10 Oct 2022
Static util testing is most simple way of testing so there’s not much to talk about it.
test('DateTimeFormatter formats correctly to api time', () {
final dateTime = DateTime(2000, 1, 1);
final result = DateTimeFormatter.formatToApiTime(dateTime);
expect(result, '2000-1-1');
});
Here we will also note that test usually have three distinctive sections:
- Given: given some setup and conditions
- When: when something happens
- Then: then we expect the result to be some value
Here’s the same test with that sections explained:
test('DateTimeFormatter formats correctly to api time', () {
// Given that dateTime is 1st January 2000
final dateTime = DateTime(2000, 1, 1);
// When we format to api time
final result = DateTimeFormatter.formatToApiTime(dateTime);
// Then we expect that resulting string is 2000-1-1
expect(result, '2000-1-1');
});
You don’t need to write these comments but it’s a good practice to leave a newline to visually separate these sections.
We’ll explain more testing concepts in further pages as we are explaining how we test interactors and presenters.