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

Unit Test NotificationCenter

by fragi
July 19, 2019
in Unit Testing
Share on FacebookShare on Twitter

In this article, we will see how we can test code that interacts with  the NotificationCenter. The Observer pattern is one of the first design patterns introduced on the famous Gang of Four book. It is a pattern that Apple uses in UIKit to notify us about events. The pattern is very useful because it decouples the sender from the receiver but overusing it can lead to difficulties to debug bugs. I use it only to subscribe to UIKit events or when I develop a framework and I have to notify the framework users.

Let’s start with our sample code. In this example we have a viewController that subscribes for “WeatherUpdate” events on viewDidLoad and unsubscribes from the event notification on the deinit method. When a “WeatherUpdate” event comes the updateWeatherForcast is being called:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class ViewController: UIViewController {
 
    var weatherForcast = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self, selector: #selector(updateWeatherForcast), name: Notification.Name(rawValue: "WeatherUpdate"), object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
 
    @objc func updateWeatherForcast(notification : Notification) {
        guard notification.name ==  Notification.Name(rawValue: "WeatherUpdate")else {
            return
        }
        guard let userInfo = notification.userInfo else {
            return
        }
        guard let weatherForcast = userInfo["weatherForcast"] as? String else {
            return
        }
        self.weatherForcast = weatherForcast
        print(self.weatherForcast)
    }
 
}

The code in its current state, is not testable. So before we start writing our unit tests we have to bring the code in a testable state. We notice that we are calling the NotificationCenter in the viewDidLoad. Our object depends on a concrete implementation. Changing from NotificationCenter to another implementation is not straightforward. In this case, we have to stop thinking of concrete implementations but to think of what is the purpose of the NotificationCenter in our object. We are using it because we want our object to subscribe and unsubscribe from notifications. By having this in mind we can capture this intent a protocol:

Swift
1
2
3
4
5
6
protocol NotificationCenterObserverProtocol {
    func addObserver(_ observer: Any, selector aSelector: Selector, name aName: Notification.Name?, object anObject: Any?)
    func removeObserver(_ observer: Any)
}
 
extension NotificationCenter: NotificationCenterObserverProtocol {}

The trick here is  that we want our  protocol to have the same method signatures as the NotificationCenter. Having the same method signatures  makes the NotificationCenter to conform to this functionality efortless.

Now, all we have to do is to make our object depend on a NotificationCenterObserverProtocol instead of the NotificationCenter. Since it is a viewController, using constructor dependency injection can be tricky, we can use property injection. The object which creates the viewController can pass to it a concrete implementation of this protocol such as a NotificationCenter. Also since he don’t have control of the NotificationCenter we can replace it with a spy(mock) that confroms to the NotificationCenterObserverProtocol protocol.

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
extension NotificationCenter: NotificationCenterObserverProtocol {}
 
class ViewController: UIViewController {
 
    var notificationCenter: NotificationCenterObserverProtocol? = NotificationCenter.default //The object which is responsible for creating the viewController should set the NotificationCenter.default.
    
    var weatherForcast = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        notificationCenter?.addObserver(self, selector: #selector(updateWeatherForcast), name: Notification.Name(rawValue: "WeatherUpdate"), object: nil)
    }
    
    deinit {
        notificationCenter?.removeObserver(self)
    }

 Having the code in a testable state, we start writing our unit tests. Let’s start by testing that the viewController subscribes for “WeatherUpdate” notifications. We can use a Spy to capture the addObserver method call:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class ViewControllerTests: XCTestCase {
    
    func test_viewDidLoad_addObserverForWeatherUpdate() {
        let sut = ViewController()
        let spyNotificationCenter = SpyNotificationCenterObserver()
        
        sut.notificationCenter = spyNotificationCenter
        
        _ = sut.view
        
        XCTAssertEqual(Notification.Name("WeatherUpdate"), spyNotificationCenter.didAddObserverWithName)
    }
}
 
extension ViewControllerTests {
    
    class SpyNotificationCenterObserver: NotificationCenterObserverProtocol {
        
        var didAddObserverWithName : Notification.Name?
        var didCallRemoveObserver = false
 
        func addObserver(_ observer: Any, selector aSelector: Selector, name aName: Notification.Name?, object anObject: Any?) {
            
            didAddObserverWithName = aName
        }
        
        func removeObserver(_ observer: Any) {
            didCallRemoveObserver = true
        }
    }
}

Now, let’s test the removeObserver method call. We can use our spy here to capture the method call. Also, we can use the addTeardownBlock that it is called after the test has finished its execution.

Swift
1
2
3
4
5
6
7
8
9
10
11
12
    func test_deinit_removeObserver() {
        let sut = ViewController()
        let spyNotificationCenter = SpyNotificationCenterObserver()
        
        sut.notificationCenter = spyNotificationCenter
        
        _ = sut.view
        
        addTeardownBlock {
            XCTAssertTrue(spyNotificationCenter.didCallRemoveObserver)
        }
    }

 We finished testing the subscribe and unsubscribe functionality. Now, we will test the method updateWeatherForcast. As we see the method has three guard clauses, so we have at least four paths to tests (3 from the guard clauses plus 1 ). 

First let’s start with the happy path:

Swift
1
2
3
4
5
6
7
8
9
func test_updateWeatherForcast_updatesWeatherForcast() {
        let sut = ViewController()
        
        let notification = Notification(name: Notification.Name(rawValue: "WeatherUpdate"), object: nil, userInfo: ["weatherForcast" : "someForcast"])
        
        sut.updateWeatherForcast(notification: notification)
        
        XCTAssertEqual("someForcast", sut.weatherForcast)
    }

Now, let’s start with the first guard clause. We want to test that if the notification name is not “WeatherUpdate” the weatherForcast is not updated:

Swift
1
2
3
4
5
6
7
8
9
10
11
func test_updateWeatherForcast_doesNotUpdateWeatherForcast_whenNotificationIsNotNamedWeatherUpdate() {
        
        let sut = ViewController()
        
        let notification = Notification(name: Notification.Name(rawValue: "aNotifcation"), object: nil, userInfo: nil)
        
        
        sut.updateWeatherForcast(notification: notification)
        
        XCTAssertEqual("", sut.weatherForcast)
    }

Now, we want to test the second guard clause. Let’s test that when the userInfo is nil the weatherForcast is not updated:

Swift
1
2
3
4
5
6
7
8
9
     func test_updateWeatherForcast_doesNotUpdateWeatherForcast_whenUserInfoIsNil() {
        let sut = ViewController()
        
        let notification = Notification(name: Notification.Name(rawValue: "WeatherUpdate"), object: nil, userInfo: nil)
        
        sut.updateWeatherForcast(notification: notification)
        
        XCTAssertEqual("", sut.weatherForcast)
    }

Now its the turn for the last guard clause. When the usrInfo does not contain the key “weatherForcast”, the weatherForcast is not updated:

Swift
1
2
3
4
5
6
7
8
9
    func test_updateWeatherForcast_doesNotUpdateWeatherForcast_whenUserInfoDoesNotContainWeatherForcast() {
        let sut = ViewController()
        
        let notification = Notification(name: Notification.Name(rawValue: "WeatherUpdate"), object: nil, userInfo: ["someKey" : "someInfo"])
        
        sut.updateWeatherForcast(notification: notification)
        
        XCTAssertEqual("", sut.weatherForcast)
    }
fragi

fragi

Related Posts

What is TDD?
Unit Testing

Write better Unit tests with XCTUnwrap

January 27, 2022

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

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...

Next Post
Unit Test Post Notification

Unit Test Post Notification

AWS: Integrate User Pools on iOS Apps

AWS: Integrate User Pools on iOS Apps

AWS: Integrate User Pools with Clean Architecture

AWS: Integrate User Pools with Clean Architecture

  • 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.