group(‘localTodos’, () {
final List todos = [
const TodoModel(id: 1, title: “Test Todo 1”, completed: false, userId: 1),
const TodoModel(id: 2, title: “Test Todo 2”, completed: true, userId: 1),
];
test('should call SharedPreferences to save the data', () async {
final expectedJsonString = json.encode(todos.map((todo) => todo.toJson()).toList());
print('Test setup complete');
when(mockSharedPreferences.setString(prefKey, expectedJsonString)).thenAnswer((_) async => true);
await localDataSource.localTodos(todos);
print('After localTodos method call');
// Ensure we verify setString was called once
verify(mockSharedPreferences.setString(prefKey, expectedJsonString)).called(1);
});
});
Was writing a test for a todo app with sharedpref
New contributor
Ed Kluivert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.