0
0
iOS Swiftmobile~10 mins

Unit testing ViewModels in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a test class for a ViewModel in Swift.

iOS Swift
class [1]: XCTestCase {
}
Drag options to blanks, or click blank then click option'
AMyViewModelTests
BViewModel
CTestViewModel
DViewModelTestCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic class name like 'ViewModel' without 'Tests'.
Not inheriting from XCTestCase (not shown here but important).
2fill in blank
medium

Complete the code to create an instance of the ViewModel inside the test class.

iOS Swift
var viewModel: [1]!

override func setUp() {
  super.setUp()
  viewModel = [1]()
}
Drag options to blanks, or click blank then click option'
AViewModelTests
BMyViewModel
CXCTestCase
DTestViewModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using the test class name instead of the ViewModel class.
Not initializing the ViewModel in setUp.
3fill in blank
hard

Fix the error in the test method to check if the ViewModel's title is correct.

iOS Swift
func testTitle() {
  let expected = "Welcome"
  XCTAssertEqual(viewModel.[1], expected)
}
Drag options to blanks, or click blank then click option'
AtitleText
Btitle()
CgetTitle()
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using method call syntax for a property.
Using a wrong property name.
4fill in blank
hard

Fill both blanks to test if the ViewModel updates its count correctly after increment.

iOS Swift
func testIncrement() {
  viewModel.increment()
  XCTAssertEqual(viewModel.[1], [2])
}
Drag options to blanks, or click blank then click option'
Acount
B1
C0
Dincrement
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the wrong property.
Expecting the count to be 0 after increment.
5fill in blank
hard

Fill all three blanks to test if the ViewModel filters items correctly.

iOS Swift
func testFilterItems() {
  let filtered = viewModel.items.filter { $0.[1].[2]([3]) }
  XCTAssertEqual(filtered.count, 2)
}
Drag options to blanks, or click blank then click option'
Aname
Bcontains
C"a"
DisEmpty
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isEmpty' instead of 'contains'.
Filtering on the wrong property.