A Note on Literals in Testing

When writing tests, it can often at first seem more convenient to use literals (such as "tomato" and 54). For example, rather than creating separate variables for each of the arguments, it may at first seem easier to write the following:

l​e​t​ ​c​o​u​r​s​e​ ​=​ ​C​o​u​r​s​e​(​t​i​t​l​e​:​ ​"​N​a​m​e​"​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​u​r​l​:​ ​N​S​U​R​L​(​s​t​r​i​n​g​:​ ​"​h​t​t​p​:​/​/​b​i​g​n​e​r​d​r​a​n​c​h​.​c​o​m​/​"​)​!​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​n​e​x​t​S​t​a​r​t​D​a​t​e​:​ ​N​S​D​a​t​e​(​)​)​

The problem with this approach, of course, is that you have to use another literal later on when you want to use the value again:

X​C​T​A​s​s​e​r​t​E​q​u​a​l​(​c​o​u​r​s​e​.​t​i​t​l​e​,​ ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.