Complete the code to declare a test class for a ViewModel in Swift.
class [1]: XCTestCase { }
The test class should be named clearly to indicate it tests the ViewModel, commonly ending with Tests.
Complete the code to create an instance of the ViewModel inside the test class.
var viewModel: [1]! override func setUp() { super.setUp() viewModel = [1]() }
The ViewModel instance should be of the actual ViewModel class, here named MyViewModel.
Fix the error in the test method to check if the ViewModel's title is correct.
func testTitle() {
let expected = "Welcome"
XCTAssertEqual(viewModel.[1], expected)
}The property to test is usually named title without parentheses if it is a property, not a method.
Fill both blanks to test if the ViewModel updates its count correctly after increment.
func testIncrement() {
viewModel.increment()
XCTAssertEqual(viewModel.[1], [2])
}The test checks if the count property equals 1 after calling increment().
Fill all three blanks to test if the ViewModel filters items correctly.
func testFilterItems() {
let filtered = viewModel.items.filter { $0.[1].[2]([3]) }
XCTAssertEqual(filtered.count, 2)
}The test filters items whose name contains the letter "a".