MVVM vs The Composable Architecture (TCA)
Classic MVVM versus Point-Free's TCA: state management, testability, complexity tradeoffs, and which architecture fits your iOS app scale.
Speed, isolation, and a reliable feedback loop
Real component interaction, end-to-end verification
The testing pyramid: lots of unit tests (70%), fewer integration tests (20%), a minimum of UI/E2E tests (10%). Use unit tests for fast feedback and integration tests to validate critical flows — together. The right strategy is to use them as complements, not substitutes for one another.
| Category | Unit Test | Integration Test |
|---|---|---|
| Performance | 10/10 | 4/10 |
| Ease of Learning | 8/10 | 6/10 |
| Ecosystem | 9/10 | 8/10 |
| Community | 9/10 | 8/10 |
| Job Market | 9/10 | 8/10 |
| Future-Proof | 9/10 | 9/10 |
// Swift - Unit test examples (XCTest + Swift Testing)
import Testing
import Foundation
@testable import MyApp
// Swift Testing framework (iOS 17+, WWDC 2024)
struct PriceFormatterTests {
@Test("Turkish lira formatting should be correct")
func turkishLiraFormat() {
let formatter = PriceFormatter(locale: Locale(identifier: "tr_TR"))
#expect(formatter.format(1234.5) == "₺1.234,50")
#expect(formatter.format(0) == "₺0,00")
#expect(formatter.format(-50) == "-₺50,00")
}
@Test("Invalid price should not be negative",
arguments: [-1.0, -100.0, -0.01])
func negativePriceValidation(price: Double) {
let validator = PriceValidator()
#expect(!validator.isValid(price))
}
}
struct CartViewModelTests {
var sut: CartViewModel!
var mockRepository: MockCartRepository!
@Test("Total price should update when a product is added")
mutating func addProductUpdatesTotalPrice() async throws {
mockRepository = MockCartRepository()
sut = CartViewModel(repository: mockRepository)
let product = Product(id: "p1", name: "MacBook", price: 75000)
await sut.addProduct(product)
#expect(sut.totalPrice == 75000)
#expect(sut.itemCount == 1)
}
@Test("Quantity should increase when the same product is added twice")
mutating func addSameProductIncreasesQuantity() async throws {
mockRepository = MockCartRepository()
sut = CartViewModel(repository: mockRepository)
let product = Product(id: "p1", name: "MacBook", price: 75000)
await sut.addProduct(product)
await sut.addProduct(product)
#expect(sut.items.count == 1)
#expect(sut.items.first?.quantity == 2)
#expect(sut.totalPrice == 150000)
}
}
// Mock implementation
class MockCartRepository: CartRepositoryProtocol {
var savedItems: [CartItem] = []
func save(_ item: CartItem) async throws {
savedItems.append(item)
}
func fetchAll() async throws -> [CartItem] {
return savedItems
}
}// Swift - Integration test examples
import XCTest
@testable import MyApp
// Integration test with in-memory SQLite
class UserRepositoryIntegrationTests: XCTestCase {
var repository: UserRepository!
var database: TestDatabase!
override func setUp() async throws {
// We use a real in-memory SQLite database
database = try await TestDatabase.inMemory()
repository = UserRepository(database: database)
}
override func tearDown() async throws {
try await database.cleanup()
database = nil
repository = nil
}
func testCreateAndFetchUser() async throws {
// Create
let userId = try await repository.createUser(
name: "Ahmet Yilmaz",
email: "[email protected]"
)
// Fetch
let fetchedUser = try await repository.fetchUser(id: userId)
XCTAssertNotNil(fetchedUser)
XCTAssertEqual(fetchedUser?.name, "Ahmet Yilmaz")
XCTAssertEqual(fetchedUser?.email, "[email protected]")
}
func testDeleteUserCascadesToPosts() async throws {
let userId = try await repository.createUser(name: "Test", email: "[email protected]")
let postRepo = PostRepository(database: database)
_ = try await postRepo.createPost(title: "Post 1", userId: userId)
_ = try await postRepo.createPost(title: "Post 2", userId: userId)
// Delete the user
try await repository.deleteUser(id: userId)
// The user's posts should be deleted too (cascade)
let posts = try await postRepo.fetchPosts(userId: userId)
XCTAssertTrue(posts.isEmpty, "Deleting the user should also delete their posts")
}
}
// Network integration test with a real URLSession
class APIClientIntegrationTests: XCTestCase {
func testFetchPublicAPIData() async throws {
// This test uses a real network connection
// Should only run in CI environments with network access
try XCTSkipUnless(ProcessInfo.processInfo.environment["INTEGRATION_TESTS"] == "1")
let client = APIClient(baseURL: URL(string: "https://jsonplaceholder.typicode.com")!)
let posts: [Post] = try await client.request(path: "/posts")
XCTAssertFalse(posts.isEmpty)
XCTAssertEqual(posts.count, 100)
}
}The testing pyramid: lots of unit tests (70%), fewer integration tests (20%), a minimum of UI/E2E tests (10%). Use unit tests for fast feedback and integration tests to validate critical flows — together. The right strategy is to use them as complements, not substitutes for one another.
Get Free ConsultationThere's no magic number. 80%+ coverage of business logic is a good target. But testing the right things matters more than the coverage percentage — blindly chasing 100% coverage is counterproductive.