SwiftUI Unit tests

up:: SwiftUI onboarding

  • Unit tests are in a separate target. They don’t get compiled and sent to the App Store
    • New > Target > Unit Testing Bundle
  • New file: Unit test class file
    • For each View, create a new test file
  • Each function must start with test
    • naming structure: test_UnitOfWork_StateUnderTest_ExpectedBehaviour
      • eg: test_UserListViewModel_isPremium_shouldBeTrue
    • testing structure:
      • Given
      • When
      • Then
@testable import MyApp // because we are in a different target, need to import app
 
func test_UserListViewModel_isPremium_shouldBeTrue() throws {
	// Given
	let userIsPremium: Bool = true
 
	// When
	let vm = UserListViewModel(isPremium: userIsPremium)
 
	// Then
	XCTAssertTrue(vm.isPremium)
}