Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Write better Unit tests with XCTUnwrap

by fragi
January 27, 2022
in Unit Testing
Share on FacebookShare on Twitter

Testing optionals can require boilerplate code to unwrap it. It can also affect the readability of the test.

Let’s look on the following example:

Swift
1
2
3
4
5
6
7
8
struct Person {
    let name: String
    let address: Address?
}
 
struct Address {
    let postCode: String
}

If we want to test the address we have to unwrap it first.

Swift
1
2
3
4
5
6
7
8
9
10
    func test_address() {
        let address = Address(postCode: "42131")
        let person = Person(name: "Fragi", address: address)
        
        if let address = person.address {
            XCTAssertEqual(address.postCode, "42131")
        } else {
            XCTFail("Failed to unwrap address")
        }
    }

We can improve the readability of the test by using  XCTUnwrap, as follows:

Swift
1
2
3
4
5
6
7
8
    func test_address_with_xctunwrap() throws {
        let address = Address(postCode: "42131")
        let person = Person(name: "Fragi", address: address)
        
        let unWrappedAddress = try XCTUnwrap(person.address)
            
        XCTAssertEqual(unWrappedAddress.postCode, "42131")
    }

fragi

fragi

Related Posts

Test static method on collaborator
Unit Testing

Test static method on collaborator

May 13, 2021

One of the main issues we are facing when we try to make an old piece of code testable is...

Dependency injection – UIViewController in Storyboard (iOS 13)
Unit Testing

Dependency injection – UIViewController in Storyboard (iOS 13)

May 13, 2021

Finally Apple (iOS 13 +) has added constructor dependency injection to Storyboards! No excuses anymore for not using DI properly...

Unit testing code with DispatchQueue
Unit Testing

Unit testing code with DispatchQueue

May 13, 2021

One way of testing code with DispatchQueue is by using expectations (https://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expectations) but in most of the cases it's more...

Unit testing UIViewController LifeCycle
Unit Testing

Unit testing UIViewController LifeCycle

May 13, 2021

Unit testing UIViewController life cycle events are not a straightforward process. Let's have a look on the following view controller:...

Dependency Injection – UIViewController with a Nib file
Unit Testing

Dependency Injection – UIViewController with a Nib file

May 13, 2021

UIViewControllers are among the most used UIKit objects. As the name implies UIViewControllers are controller objects and their purpose should...

Unit Test Post Notification
Unit Testing

Unit Test Post Notification

July 19, 2019

In this article, we will see how we can test code that post notifications. Let's see our example code: ...

Next Post
Separate configuration from code – xcconfig

Separate configuration from code - xcconfig

Separate configuration from code – xcconfig

XcodeGen

BIP39

BIP39

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.