Controlling time in a unit test allows you to test code that depends on the passage of time like Timer or time comparisons. Dart makes it easy as long as you use the Clock package in code that gets dates instead of using DateTime directly. For any code using Clock time can be controlled through the FakeAsync package. Example Time Test Two key things to note in this example test. First that the test is wrapped in the fakeAsync method. Second that the first time clock is used is inside the fakeAsync wrapper. That’s important, if you access the clock [Read on…]
Dart added the late keyword as part of their null safety release and it is very useful, particularly for testing. As a quick review null safety means telling the compiler that some variables will never be null. Any variable declared without a question mark following the type declaration is null safe. So String name is null safe while String? name can be null. Given that the question mark is new syntax all code written before null safety was introduced now declares all variables as cannot be null. The new late keyword tells the compiler that although a variable is not [Read on…]
After the upgrade to Flutter 1.20.1 none of my widget tests worked anymore. On investigation I found that my widgets weren’t getting rendered at all, i.e. their build method wasn’t being called. This is a preliminary result but I was doing this in my test: Seems like that no longer works and must now be: I haven’t seen anything in the release notes that suggests this should be required. I’m still investigating but in case this helps someone else I thought I’d put it out there.
Like all programmers I have a pretty long held grudge against daylight savings time, leap years, time zones, and just generally anything to do with dates and times. The ways in which date complexity have hurt over the years are many and varied. This year’s pain provided a good opportunity to use the excellent unit test support for time in Dart. This post is a tiny bit about date calculations and mostly about how to get control over dates in tests. What Went Wrong This Year This is the history and context part of the post. If you just want [Read on…]
Every once in a while the value of a coding practice like unit testing is reinforced, yesterday was one of those days so I wanted to share with my future self in case I ever start to doubt. Also, the set of technical practices that I first learned as XP (Extreme Programming), are not as common as I used to think, so this is my small contribution to improving that. The Problem The code was using an ID received from an external source as an internal unique identifier. The identifier was assumed to be unique across objects of the same [Read on…]
Creating plugins for Flutter is well documented and pretty easy. Unit testing the resulting native code in iOS wasn’t quite as straightforward so I thought I’d capture what I did while I still remember the steps. This post shows how to create a new plugin for Flutter with a Swift implementation for iOS, and then add Swift XCTest unit tests for that implementation. Creating a new plugin To create my first plugin I followed the Flutter docs, in particular the section on Developing plugin packages. I used the version of the create command that specified the native languages, like so: [Read on…]