Using ViewInspector to test a simple view
The view we are going to build will be used to add new to-do items to the list of items. This means it needs input fields for all information a to-do item can hold. So, let's look into that aspect in the next subsections.
Adding a title text field
As always, we start with the test. Follow these steps to add a text field for the title of a to-do item to the input view:
- Select the ToDoTests group in the project navigator and add a Unit Test Case Class with the name
ToDoItemInputViewTests
. Remove the two template test methods. - Import the
ViewInspector
library and the main target (ToDo
) so that it is testable (@testable
):// ToDoItemInputViewTests.swift import XCTest @testable import ToDo import ViewInspector
- Before we can write tests for a SwiftUI view, we first need to extend it with the
Inspectable
protocol from theViewInspector
library. Add the following line right above the test case class:// ToDoItemInputViewTests...