Widget testing passed in function

Search for a command to run...

No comments yet. Be the first to comment.
Here we will cover various aspects of unit, widget and integration testing in Flutter
Background When I started building out the new mobile app for Wyztalk there were a few things on my personal ToDo list that obviously had great benefit to the business, one was a CI pipeline (Article) which I recently completed, this one, however, wa...
The Problem Wasn’t Triage Most AI workflows in software engineering still keep the human directly in the middle of triage. The AI might help write code. It might explain a stack trace. It might summar

There’s been a steady undercurrent of people switching tools lately. Copilot to Claude. Claude to something else. Codex quietly entering more workflows. A lot of confidence in different directions, of

It’s been said to me that at times my responses can come across as a “no” first-unintentionally, of course, but real all the same. Not long ago, someone on my team shared an idea they were exploring. I began with a caution, meaning to help them avoid...

The opening sentence of any message often carries more weight than everything that follows. I’ve learned this the hard way. Recently, a report shared something he wanted to pursue. I was supportive — I really was — but my first response came as a cau...

One of the things I’ve always been comfortable with is admitting when I don’t know something. To me, it’s one of the real distinctions between the more and less experienced engineers — and honestly, people in general. When you’re earlier in your care...

In this post, we going to go through how, at least in my opinion, one would go about testing that a function is called within a widget when that function is one of the Widgets arguments.
Take the following overly simplified example…
class SampleWidget extends StatelessWidget {
final Function(String fileId) onDelete;
final String fileId;
const SampleWidget({
@required this.onDelete,
@required this.fileId,
foundation.Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
throw FlatButton(
onPressed: () => onDelete(fileId),
child: const Text('Delete'),
);
}
}
As you can see we have a simple “reusable” widget that takes a fileId and a onDelete function.
Testing this in the implementing widget is quite simple as you would simply verify the resulting function is called.
However, if you wished to test this widget in isolation, there are a few more steps required.
In our current project, we added a very simple helper class:
class TestCallbackFunctions {
void onFileDelete(String fileId) => null;
}
This class will then hold simply “mock” functions that we can pass into reusable widgets to test it completely.
I hope you found this interesting, and if you have any questions, comments, or improvements, feel free to drop a comment. Enjoy your Flutter development journey :D
If you liked it, a like would be awesome, and if you really liked it, a cup of coffee would be great.
Thanks for reading.
If you wish to carry on with the subject of testing, why not take a look at: