The initial view controller

When you build and run the app now on the simulator, you will only see a black screen. The reason for that is we haven't specified which screen the app should show after it is started. Let's write a test for this. Because this is a test about the storyboard, add iOS | Source | Unit Test Case Class to the test target and call it StoryboardTests. Import the main module using the @testable keyword and remove the two template tests.

Add the following test to StoryboardTests:

func test_InitialViewController_IsItemListViewController() { 
  let storyboard = UIStoryboard(name: "Main", bundle: nil)
 
   
  let navigationController = 
    storyboard.instantiateInitialViewController() 
      as! UINavigationController 
  let rootViewController = navigationController.viewControllers[0]
 
   
  XCTAssertTrue(rootViewController is ItemListViewController) 
} 

This test gets a reference to the Main storyboard, instantiates its initial view controller (which should be a navigation controller), and gets its root view controller. Then, it asserts that the root view controller is of the type ItemListViewController.

Run the test. The test crashes with an error: unexpectedly found nil while unwrapping an Optional value in the line where we try to initialize the initial view controller. The reason for this is that we have not told Xcode what the initial view controller is.

Open Main.storyboard, select the item list view controller and open the Attribute Inspector. Check the checkbox next to Is Initial View Controller, as shown in the following screenshot:

With the item list view controller still selected, navigate to Editor | Embed In | Navigation Controller. With these changes in the storyboard, the initial view controller will be a navigation controller with an instance of ItemListViewController as its root view controller.

Run the tests again. All the tests pass and there is nothing to refactor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.78.137