<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Clue</title>
    <description>The latest articles on DEV Community by Clue (@clue).</description>
    <link>https://dev.to/clue</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F215%2F3133baa8-cad0-4e1b-8987-02400f0fa3be.png</url>
      <title>DEV Community: Clue</title>
      <link>https://dev.to/clue</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/clue"/>
    <language>en</language>
    <item>
      <title>Writing better unit tests in Swift: Part Two</title>
      <dc:creator>Clue</dc:creator>
      <pubDate>Fri, 16 Mar 2018 10:33:21 +0000</pubDate>
      <link>https://dev.to/clue/writing-better-unit-tests-in-swift-part-two-4pdo</link>
      <guid>https://dev.to/clue/writing-better-unit-tests-in-swift-part-two-4pdo</guid>
      <description>&lt;p&gt;&lt;em&gt;Validating your code with custom assertions&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AzKuWcfqLosfVWjD_vPxieA%402x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AzKuWcfqLosfVWjD_vPxieA%402x.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://dev.to/clue_staff/writing-better-unit-tests-in-swift-part-one-1o09-temp-slug-9962621"&gt;the first part of this series&lt;/a&gt; I defined what I think of as a “good” unit test. This is the definition that I ended up at:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If we can agree that a unit test (or, really, any test) is composed of some &lt;em&gt;setup&lt;/em&gt;, the &lt;em&gt;action&lt;/em&gt; that we’re testing, and then an &lt;em&gt;assertion&lt;/em&gt; about the effect of that action, then I would say that, put simply, a “good” unit test is one which makes each of those three components clear.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might also remember that we finished that post with tests that looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var sut, other: User!

func test\_equals\_allPropertiesMatch\_isTrue() {
 (sut, other) = (.create(), .create())
 XCTAssertEqual(sut, other)
 XCTAssertEqual(other, sut)
}

func test\_equals\_nameDiffers\_isFalse() {
 (sut, other) = (.create(name: "Jo"), .create())
 XCTAssertNotEqual(sut, other)
 XCTAssertNotEqual(other, sut)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We compressed the setup so that the test body contained only the information that was important to the scenario of the test. For example, when all that mattered was that the names of the two user objects differed, we included only that information in the setup.&lt;/p&gt;

&lt;p&gt;This goes a great way to improving the overall readability of the test. But there’s even more we can do.&lt;/p&gt;

&lt;p&gt;It’s possible that you looked at the above tests and thought “the setup’s okay, but why the heck have you got two asserts there?” Great question! I’m so glad you’re paying attention.&lt;/p&gt;

&lt;p&gt;Remember that the above tests are covering the == function on a User type. Without dropping a whole load of maths on you, equality is an example of an “&lt;a href="https://en.wikipedia.org/wiki/Equivalence_relation" rel="noopener noreferrer"&gt;equivalence relation&lt;/a&gt;” and one of the important things about being an equivalence relation is the concept of symmetry. Put simply, if I have two instances a and b of some type, it should never be the case that a == b is true but b == a is false. If this is possible, then our definition of equality is flawed. So it makes sense to validate that our custom definition of == is symmetric.&lt;/p&gt;

&lt;p&gt;But simply looking at our two asserts there, it’s absolutely not clear that this is the intention. These tests are only really asserting one thing. It’s possible that, with some time and the inclination, other engineers could work out why we added the second assert, but we can definitely do better.&lt;/p&gt;

&lt;p&gt;But…how?&lt;/p&gt;

&lt;h3&gt;
  
  
  The Solution
&lt;/h3&gt;

&lt;p&gt;Let’s start by doing the simplest thing we can possibly think of. We have two lines of code, and we’d like to have only one line of code. The solution? A function!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func assertSymmetricallyEqual(\_ sut: User, \_ other: User) {
 XCTAssertEqual(sut, other)
 XCTAssertEqual(other, sut)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Note: we’re focusing in on testing for equality here, but you can do the exact same thing with non-equality).&lt;/p&gt;

&lt;p&gt;Now our test becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var sut, other: User!

func test\_equals\_allPropertiesMatch\_isTrue() {
 (sut, other) = (.create(), .create())
 assertSymmetricallyEqual(sut, other)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run this test and it passes, everything’s good. But when the assertions fail, Xcode shows the failures in the wrong place.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F452%2F1%2ALOEtYibl8_EdWNsDk9Nl3A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F452%2F1%2ALOEtYibl8_EdWNsDk9Nl3A.png"&gt;&lt;/a&gt;N.b. I had to change the setup to force the test to break.&lt;/p&gt;

&lt;p&gt;Clearly this isn’t acceptable. If we’re trying to diagnose a failing test quickly, we want to be able to see exactly what’s failed and where. More to the point, if two or three tests all fail using this same assert function then we’re going to have a hard time separating the failures out.&lt;/p&gt;

&lt;p&gt;Fortunately, the XCTAssertEqual &lt;a href="https://developer.apple.com/documentation/xctest/2142776-xctassertequal" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; provides a solution. There we can see the Swift declaration of the function, which is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func XCTAssertEqual\&amp;lt;T\&amp;gt;(
 \_ expression1: @autoclosure () throws -\&amp;gt; T, 
 \_ expression2: @autoclosure () throws -\&amp;gt; T, 
 \_ message: @autoclosure () -\&amp;gt; String = default, 
**file: StaticString = #file,   
 line: UInt = #line** ) where T : Equatable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I’ve made the two most important lines bold.&lt;/p&gt;

&lt;p&gt;Since XCTAssertEqual is a function in Swift (rather than a macro, as it was in Objective-C), there’s a slight trick to get Xcode to render failures in the correct place. When we call XCTAssertEqual the Swift compiler adds the file it was called from (#file), and the line it was called on (#line), to the call as default arguments.&lt;/p&gt;

&lt;p&gt;Since our calls to XCTAssertEqual don’t happen on the line we want to see the failure on, we need to put in a bit of extra effort to get everything working. Essentially, we need to tell XCTAssertEqual the correct file and line to show failures on. This should be the #file and #line that our custom assert method is called on. So, following the example from the Apple documentation, we end up with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func assertSymmetricallyEqual(
 \_ sut: User, \_ other: User,
 **file: StaticString = #file,  
 line: UInt = #line**  
) {
 XCTAssertEqual(sut, other, **file: file, line: line** )
 XCTAssertEqual(other, sut, **file: file, line: line** )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The changes to the method have been marked in bold.&lt;/p&gt;

&lt;p&gt;Running the tests again, we get the following result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F471%2F1%2AQ0DBTd6g5o3C9Y_lDa-qQQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F471%2F1%2AQ0DBTd6g5o3C9Y_lDa-qQQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright! This is exactly what we were looking for. Now the failures show up exactly where we need to see them. 🙌&lt;/p&gt;

&lt;h3&gt;
  
  
  The Refactor
&lt;/h3&gt;

&lt;p&gt;What we’ve done above is fine, but as soon as we want to assert that more than one type satisfies symmetric equality we’re going to find ourselves with a one way ticket to Duplication Town. 🏘🏘🏘🏘&lt;/p&gt;

&lt;p&gt;The solution? For that we can refer back to the &lt;a href="https://developer.apple.com/documentation/xctest/2142776-xctassertequal" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; from earlier. Swift’s XCTAssertEqual method is generic over some T: Equatable — so let’s just do the exact same thing to our assertSymmetricallyEqual.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func assertSymmetricallyEqual **\&amp;lt;T: Equatable\&amp;gt;** (
 \_ sut: **T** , \_ other: **T** ,
file: StaticString = #file,  
 line: UInt = #line  
) {
 XCTAssertEqual(sut, other, file: file, line: line)
 XCTAssertEqual(other, sut, file: file, line: line)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changes marked in bold again. And look at that! There’s hardly any of them!&lt;/p&gt;

&lt;p&gt;Now we have a nice, reusable custom assert that confirms that two instances of an Equatable type are equal. What a time to be alive!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Bonus
&lt;/h3&gt;

&lt;p&gt;At Clue, we’ve actually taken this pattern a small step further. We have an internal framework which we use for test helpers which are needed across different modules. This framework contains the following struct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public struct Assert\&amp;lt;T\&amp;gt; {
 private let subject: T?

 init(\_ subject: T?) {
 self.subject = subject
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;“Alright, great,” I hear you cry, “but this does absolutely nothing.” And you’re right! Because the real magic happens in the extensions to this type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension Assert where T: Equatable {
 public func symmetricallyEqual(
 to other: T, 
 file: StaticString = #file, 
 line: UInt = #line
 ) {
 XCTAssertEqual(subject, other, file: file, line: line)
 XCTAssertEqual(other, subject, file: file, line: line)
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And when we need to call this method, it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assert(sut).symmetricallyEqual(to: other)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Beautiful!&lt;/p&gt;

&lt;p&gt;And, as a special treat, here’s an example of how we can extend this pattern to other types of assert. Here’s my own personal favourite custom assert function, which lets us assert that a specific error was thrown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension Assert where T: Error &amp;amp; Equatable {
 public func isThrownIn(
 \_ expression: @autoclosure () throws -\&amp;gt; (),
 file: StaticString = #file,
 line: UInt = #line
 ) {
 XCTAssertThrowsError(
 try expression(), file: file, line: line
 ) {
 XCTAssertEqual(
 subject, $0 as? T, 
 file: file, line: line
 )
 }
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(I had to take the indentation to slightly absurd levels to get this to fit in a Medium code block. Sorry!)&lt;/p&gt;

&lt;p&gt;We can then use this as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assert(Errors.someError).isThrownIn(try someMethodThatThrows())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oooh! ✨&lt;/p&gt;




</description>
      <category>iosappdevelopment</category>
      <category>unittesting</category>
      <category>swift</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Writing better unit tests in Swift: Part One</title>
      <dc:creator>Clue</dc:creator>
      <pubDate>Tue, 27 Feb 2018 09:58:18 +0000</pubDate>
      <link>https://dev.to/clue/writing-better-unit-tests-in-swift-part-one-41if</link>
      <guid>https://dev.to/clue/writing-better-unit-tests-in-swift-part-one-41if</guid>
      <description>

&lt;p&gt;&lt;em&gt;Generating test data with factory methods&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yjFnpFHj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AOBYnnEGuTxwoFPsGjgvlUg%402x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yjFnpFHj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AOBYnnEGuTxwoFPsGjgvlUg%402x.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The problem
&lt;/h3&gt;

&lt;p&gt;Look, we’re amongst friends here, so I feel I can be totally honest: I’ve written some really awful unit tests in my career. Twenty-line monsters with multiple mocks and asserts and asynchronous expectations. The sort of thing you see in books with titles like “How to Fix the Mess Left by the Idiot Who Worked Here Before.” I’ve also had to maintain that code after I’d written said unit tests and the less said about that the better. So suffice to say I make it a priority now to write “good” unit tests.&lt;/p&gt;

&lt;p&gt;Before we get started, I’ll define what I think of as a “good” unit test. If we can agree that a unit test (or, really, any test) is composed of some &lt;em&gt;setup&lt;/em&gt;, the &lt;em&gt;action&lt;/em&gt; that we’re testing, and then an &lt;em&gt;assertion&lt;/em&gt; about the effect of that action, then I would say that, put simply, a “good” unit test is one which makes each of those three components clear.&lt;/p&gt;

&lt;p&gt;(Aside: this may differ from your definition of a “good” unit test, and that’s just something we’ll both have to live with.)&lt;/p&gt;

&lt;p&gt;Over the course of a few blog posts I’m going to show you some of the things we do at Clue to ensure we’re always trying to write “good” unit tests. In this post we’re going to look at a simple trick we can use to minimise the &lt;em&gt;setup&lt;/em&gt; part of our unit tests, while maintaining clarity.&lt;/p&gt;

&lt;p&gt;Let’s say we want to unit test the equality method on a simple Swift struct.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Equatable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;

&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;
 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;needsVerification&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;needsVerification&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are (countably) infinitely many different argument combinations we could pass into this method for testing. Obviously we can’t test with all of them, so we have to pick a few cases that represent general trends in results. A valid way of unit testing this method (and, indeed, the way I would normally write such a method, using test-driven development) would be to start with the case where all properties of the two Users are equal, and then to test what happens when each individual property differs. This gives us a suite of tests like this:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_equals&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_allPropertiesMatch&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="nf"&gt;_isTrue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;sut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;other&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_equals&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_nameDiffers&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="nf"&gt;_isFalse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;sut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Jo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;other&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_equals&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_emailDiffers&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="nf"&gt;_isFalse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;sut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;other&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;needsVerifiation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_equals&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_needsVerificationDiffers&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="nf"&gt;_isFalse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;sut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;other&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(sut stands for “subject under test” and for some reason it’s the only acronym variable name I’m okay with.)&lt;/p&gt;

&lt;p&gt;These aren’t bad unit tests by any stretch of the imagination — in fact they’re basically fine — but they’re also not “good.” Why not?&lt;/p&gt;

&lt;p&gt;Let’s look at the setup of our two Users. Each time we create them we have to pass in all of the properties that the init method expects. This is true even if, in the context of the test, we don’t actually care what they are. In the first test, for example, all we care about is that the properties Match — their values could be the names of the Spice Girls in ascending height order, or my favourite Berlin coffee shops (Bonanza &amp;amp; Five Elephant btw), and it would make no difference to the test so long as they were the same. Similarly in the other three tests we only care that one specific property Differs.&lt;/p&gt;

&lt;p&gt;Having information in these tests that we don’t care about creates noise, and makes the tests harder to understand. That’s maybe okay for these small examples, but when you’re trying to work out why a more complicated unit test is failing an hour before you cut a release, you’ll be very thankful you cut out as much unnecessary noise as possible.&lt;/p&gt;

&lt;p&gt;Wouldn’t it be just super great and awesome and peachy and other superlatives if our tests only contained pertinent information? And, of course, if that wasn’t possible all of this set up would have been for nothing.&lt;/p&gt;

&lt;h3&gt;
  
  
  The solution
&lt;/h3&gt;

&lt;p&gt;So here’s what we’re going to do: we’re going to extend the User type in our test target in order to add a method that lets us configure a User with the data we care about, while using sensible defaults for the other properties. I tend to call this method create because I think it reads nicely. Whatever you call it, the method should look something like this:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
 &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
 &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
 &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;needsVerification&lt;/span&gt;
 &lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(Don’t worry, I don’t like the way you space out your code either.)&lt;/p&gt;

&lt;p&gt;These create methods are really simple — they take in the same arguments as the type’s init method but give each one a default value. As a rule of thumb I’d tend to use "" for String arguments, 0 for numeric ones, false for Bools, etc. Whatever you decide to use, the really important thing is to try and keep it consistent across different create implementations that you write.&lt;/p&gt;

&lt;p&gt;(Cool aside: once a few of your types have create methods you can start using the result of calling create without any arguments as default values too. So if you had Account(user: User) then in Account.create you could set the default value for the user argument to be User.create().)&lt;/p&gt;

&lt;p&gt;Now, combined with Swift’s type inference abilities we can rewrite the above tests like so:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;other&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_equals&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_allPropertiesMatch&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="nf"&gt;_isTrue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_equals&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_nameDiffers&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="nf"&gt;_isFalse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Jo"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_equals&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_emailDiffers&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="nf"&gt;_isFalse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_equals&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;_needsVerificationDiffers&lt;/span&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="nf"&gt;_isFalse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;needsVerification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="kt"&gt;XCTAssertNotEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Pretty nice, right? Now each test only contains the information that’s actually relevant to the specific test case. This is a Good Thing.&lt;/p&gt;

&lt;p&gt;Anyway, that’s a brief introduction to writing nice factory methods in Swift. This is obviously just one small way you can improve your unit tests, but you’ve got to start somewhere right? Go forth now, and write unit tests. ✅&lt;/p&gt;

&lt;p&gt;(Thanks to &lt;a href="http://joshuaheald.com"&gt;Josh Heald&lt;/a&gt;. We were pairing when this pattern for writing factory methods initially came together.)&lt;/p&gt;





</description>
      <category>unittesting</category>
      <category>tdd</category>
      <category>softwaredevelopment</category>
      <category>iosappdevelopment</category>
    </item>
    <item>
      <title>Nicer reuse identifiers with protocols in Swift</title>
      <dc:creator>Clue</dc:creator>
      <pubDate>Thu, 07 Dec 2017 10:44:30 +0000</pubDate>
      <link>https://dev.to/clue/nicer-reuse-identifiers-with-protocols-in-swift-15nk</link>
      <guid>https://dev.to/clue/nicer-reuse-identifiers-with-protocols-in-swift-15nk</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VE8MOxz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Axygb5sEsnYtGDvmGcdv9fA%402x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VE8MOxz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Axygb5sEsnYtGDvmGcdv9fA%402x.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently I was going through the motions of setting up a new UICollectionView. I had written a view model for my cells, and I had a UICollectionViewCell subclass all ready to go. All that was left to do was to implement cellForItem(at:).&lt;/p&gt;

&lt;p&gt;As an aside: if you’ve not worked with UICollectionView before, but are more of a UITableView kinda person, then you can just replace Collection with Table and Item with Row and this post should still be valid.&lt;/p&gt;

&lt;p&gt;As a responsible iOS engineer [citation needed] I knew that the first thing I needed to do here was ask my collectionView to dequeue a cell. And to do that I had to call dequeueReusableCell(withReuseIdentifier:for:), passing a String “reuse identifier.” In order for the collectionView to have any idea what I was talking about, I also had to call register(_:forCellWithReuseIdentifier:) on the collection, so it knew to map this reuse identifier to my UICollectionViewCell subclass.&lt;/p&gt;

&lt;p&gt;That subclass looked roughly like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;final class CustomCollectionViewCell: UICollectionViewCell {

// code etc.

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another aside: CustomCollectionViewCell is just an example name. Please never name classes like this.&lt;/p&gt;

&lt;p&gt;I decided a sensible reuse identifier for this cell would be "CustomCollectionViewCell". So I used that, and called both the methods. This is what those calls looked like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// in a set up method
collectionView.register(
 CustomCollectionViewCell.self, 
 forCellWithReuseIdentifier: "CustomCollectionViewCell"
)

// in cellForItem(at:)
collectionView.dequeueReusableCell(
 withReuseIdentifier: "CustomCollectionViewCell"
 for: indexPath
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is obviously awful. We’ve got a magic string hanging around there, and it’s in more than one place. Clearly this is in need of some refactoring, so that’s exactly what I did. The first step was simply to move this string into a static constant on the CustomCollectionViewCell class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;final class CustomCollectionViewCell: UICollectionViewCell {

static let reuseIdentifier = "CustomCollectionViewCell"

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, good start. This means we can access the identifier anywhere in the code using CustomCollectionViewCell.reuseIdentifier. Already a massive improvement over what we had before. But it does present another problem. What if the class name changes later on?&lt;/p&gt;

&lt;p&gt;We could just remember to change it, but let’s be real here: we’re humans, and we don’t always remember things like this. Even when it’s staring us right in the face. That’s why we get computers to do this stuff for us. So that leads nicely on to the next question: can we get the compiler to help us?&lt;/p&gt;

&lt;p&gt;Well, if the answer was no, this would be an awfully short blog.&lt;/p&gt;

&lt;p&gt;So how do we do it? See above, where we passed the reference to the CustomCollectionViewCell type into register(_:forCellWithReuseIdentifier:)? Wouldn’t it be great if we could convert that type into a String somehow and just use that? That would be so great.&lt;/p&gt;

&lt;p&gt;(Dramatic pause.)&lt;/p&gt;

&lt;p&gt;Yep, of course we can do exactly that. We can replace the bare String in the definition of reuseIdentifier with String(describing: CustomCollectionViewCell.self) and get exactly the same output. Now if the class name changes, the compiler won’t recognise the type there, and will let us know about it. Success!&lt;/p&gt;

&lt;p&gt;Okay, right about now you might be looking at what we’ve done so far and thinking: “Matthew, this is great and all, but the title says ‘protocols’ and so far we’ve just pissed about with a bit of refactoring.” And you’re right, for sure. Consider all of the above simply as motivation for what comes next.&lt;/p&gt;

&lt;p&gt;So after a while of using String(describing: TypeName.self) to generate reuse identifiers, I started to wonder if there was a way of doing this that would remove the boilerplate of defining a new reuseIdentifier in every cell subclass.&lt;/p&gt;

&lt;p&gt;I started by writing the following protocol:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protocol ReuseIdentifying {
 static var reuseIdentifier: String { get }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then extending it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension ReuseIdentifying {
 static var reuseIdentifier: String {
 return String(describing: /\* er... what goes here? \*/)
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I got a bit stuck here for a while. I needed a way to dynamically refer to the type that was implementing the protocol. Obviously, using ReuseIdentifying.self wouldn’t work, because that’s just going to return "ReuseIdentifying" every time.&lt;/p&gt;

&lt;p&gt;In the past I’ve used Self in protocols to indicate “the implementing type.” So I wondered whether I could get away with something like Self.self. It looks utterly ridiculous, but…&lt;/p&gt;

&lt;p&gt;Spoiler: it totally worked.&lt;/p&gt;

&lt;p&gt;So here’s the final protocol extension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension ReuseIdentifying {
 static var reuseIdentifier: String {
 return String(describing: Self.self)
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now by applying that protocol to my CustomCollectionViewCell I can delete the reuseIdentifier definition, and everything works as expected. The returned reuseIdentifier is, as I wanted, "CustomCollectionViewCell".&lt;/p&gt;

&lt;p&gt;And there’s even better news: we can go further with this. Let’s say we want every single UICollectionViewCell subclass to have a reuseIdentifier. We don’t actually need to individually apply this protocol. We can just do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension UICollectionViewCell: ReuseIdentifying {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Magic! ✨&lt;/p&gt;

&lt;h3&gt;
  
  
  Open questions
&lt;/h3&gt;

&lt;p&gt;I like to test things, so I wanted to write some unit tests against this protocol extension.&lt;/p&gt;

&lt;p&gt;I tried defining a class in the body of a test. Something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func test\_reuseIdentifier\_classIsCalledSomeClass\_isSomeClass() {
 class SomeClass: ReuseIdentifying {}
 XCTAssertEqual("SomeClass", SomeClass.reuseIdentifier)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As far as I could see, there’s no reason this wouldn’t work. But String(describing:) starts to behave kind of confusingly at this point. The return value from reuseIdentifer is "SomeClass #1".&lt;/p&gt;

&lt;p&gt;I thought this might be an issue with having the class defined inside the method, so I moved it up to the file scope and ran the test again. And it worked!&lt;/p&gt;

&lt;p&gt;So, if anyone knows why defining the class inside a method leads to the "#1" being appended to the end of the description, I’d be super interested to find out.&lt;/p&gt;




</description>
      <category>software</category>
      <category>engineering</category>
      <category>development</category>
      <category>swift</category>
    </item>
  </channel>
</rss>
