4/24/2022
I’ve been chipping away at Unit Testing Liquidus.
I started with my models being Drink
, DrinkType
, DataItem
, and DrinkData
.
It was a good thing I already conformed Drink
, DrinkType
, and DataItem
to the Equatable
protocol so I can just do XCTAssertEqual(drinkA, drinkB)
without having to go through each property every time. Interestingly, because DrinkData
is a struct, not a class, I don’t have to do anything besides since it conforms to Equatable
. Nor do I have to do any extra work when comparing [Drink]
, [DrinkType]
, and [DataItem]
arrays.
I did forget how Date
s work. I can’t just do dateA == dateB
and expect it to evaluate to true assuming they are the same. You have to use Calendar.current.compare()
. My best guess as to why would be similar to how Strings work in Java. If stringA
and stringB
are the same stringA == stringB
will still return false because these two variables are stored in different places in memory. So you’d have to use stringA.equals(stringB)
. But that’s just my speculation with this.
In Java, the toString()
method dictates how data types are converted to Strings. I wanted to see if something similar to this existed in Swift. I thought it could be useful to add more detailed test failure messages. If you make a class (probably a struct too but I didn’t try it) conform to CustomStringConvertible
, you will be able to set the description
property of your class as a computed property to return a specified String when the type is converted to a String.
In terms of the models, I tested the constructor by checking each property of the class. I also tested the Equatable
code.
Then to DrinkModel
. I did have to add a test
parameter to it so it won’t read the stored data and just create a brand new DrinkData
. So far I’ve written tests for the constructor where the test
parameter is set to true
, the addDrink()
method, and the getDataItems
methods for a day, week, and month.
For each getDataItems
test they were split into two tests, one where all drinks regardless of types are used and one where one Water was filtered for. I had to create a method for creating and adding the drinks to the data store and one to get the expected results.
I got some 79 more methods to go in my DrinkModel
with some work already started on the getDataItemsForHalfYear()
method. Got a lot of work ahead of me.