mokacoding

unit and acceptance testing, automation, productivity

Posts tagged "swift"

XCTNSPredicateExpectation is slow, and what to do about it

Each XCTNSPredicateExpectation requires a timeout of at least 1.1 seconds. That's will unnecessarily slow down your test suite. You can use Nimble's toEventually instead and make your tests as fast as possible.

When to test a @Published property using sink

Swift @Published properties come with an associated Combine Publisher that emits values over time. This free XCTest tutorial explains when to write a unit test that accesses the property directly and when it's instead necessary to subscribe to it using the sink operator.

A real-world example of TDD catching bugs

I published a YouTube video with a tutorial on implementing the FizzBuzz algorithm using Test-Driven Development. While recording, I made a couple of thinking or coding mistakes, and, sure enough, the tests immediately pointed them out.

How to write unit tests for SwiftUI apps

To test SwiftUI applications, don't test SwiftUI code. The SwiftUI framework doesn't lend itself to writing unit tests so don't try to shoehorn views in your test harness. Instead, split layout declaration form content generation logic.

Test-Driven Development in Swift is now available

My book, Test-Driven Development in Swift, is now available in online bookstores everywhere. You'll learn Test-Driven Development writing a real-world SwiftUI application, including events-flow management with Combine, networking, local storage, and third-party libraries.

WWDC21: What's New in Testing

A roundup of the testing-related new features announced at WWDC 2021. Including Xcode Cloud, how to test code using async/await, the new XCTExpectFailure and addTearDownBlock APIs, and the new Test Repetition configuration in Test Plans.

How to test Swift async/await code with XCTest

Swift 5.5 and Xcode 13 introduce the async/await pattern for concurrent code. This tutorial post shows how to write unit tests for asynchronous code in Swift using the XCTest framework.

How to write better tests for Swift date comparisons

Testing Swift date comparison code with XCTest can result in indeterministic tests because of the passage of time. To make tests robust and deterministic, decouple them from the system clock by injecting the reference date.

How to manage complex inputs in your Swift tests with Scenario Builders

When writing unit tests in Swift for complex objects, you may need to write a lot of setup boilerplate code in the arrange phase. Scenario Builders are a pattern that extracts and encapsulated all that logic in a single component with an English-like API. This tutorial shows how to build a Scenario Builder in Swift and looks at its pros and cons.

How to set default values in Swift compiler-generated initializers

You can get the Swift compiler to generate an initializer with default values for your structs, if you're willing to put up with a bit of mutability.

Write better Swift unit tests with custom XCTest assertions

The XCTest Swift testing framework has a limited offer of assertions. There's only so much you can do with XCTAssertTrue and XCTAssertEqual. This XCTest tutorial shows how to create custom assertions to make your unit tests and UI tests shorter and clearer.

How to write unit test assertions for Swift Result values

Result is one of the most useful types in the Swift language. Learn how to write better unit tests using Result in this XCTest tutorial.

How to bypass the SwiftUI App when running unit tests

How to make the unit tests of your SwiftUI app safer and faster by preventing them from running the program startup flow. This will avoid all of the launch operations like network requests or reads from the local storage that would affect the global state.

How to make the View to ViewModel relationship clear

Using Swift's nested types helps making it clear that a view model belongs to a view.

What can a pipe wrench teach us about software engineering?

With his famous pipe wrench lecture, Vannevar Bush taught young MIT engineers the value of precision. The same teaching holds true for software developers.

Replace Triple-state Boolean with Enumeration

Triple-state Booleans can be ambiguous to work with. Replace them with an enum to make the code clearer.

How to decouple unit tests from values that change frequently

When the output value of a function changes often but the logic to pick it doesn't, adding a separation layer will make unit tests easier to maintain.

Referential Transparency in Swift

An explanation of what referential transparency means with examples in Swift

How to test view controllers navigation

The answer to "How can I test that a view controller presents another view controller when something happens?" is as simple as defining a delegate.

Better tests for delegates

When testing delegates, we are asserting rigid implementation details. Here's a way to make those tests more flexible.

How to TDD in Swift, a step by step guide

With test driven development you can write high quality software in small shippable steps. Here's how to get started.

Test doubles in Swift: dummies, fakes, stubs, and spies.

An overview of the different kind of doubles we can use in our tests, and how to write them in Swift.

How to split decision and action logic with the Swift type system

There is a subtle way to overload software components, by making them both take decision and act on them. We can simplify these bloated components by separating the responsibility of taking decisions from the one action on them. This will result in leaner and easier to maintain software, and is made simple by the Swift type system.

How to remove duplication from Swift tests with helper functions

Some code ends up requiring a lot of duplication to be tested. You can remove it by using helper functions encapsulating the shared assertion logic.

Streamlining tests setup with fixtures in Swift

Keeping tests short and focused is important for the health of the test suite. A fixture method to generate instances with default values in the tests helps keeping the setup code short, focused, and readable

Action focused protocols enhance testability

Using protocols describing a single capability or action that can be performed is a way to enhance local reasoning and facilitate testability

Nimble: when to use waitUntil or toEventually

The Nimble matchers framework provides two ways assert expectations on asynchronous code, this post explores when to use one or the other.

Unless.swift

Porting Ruby's unless operator into Swift via a function.

XCTest closure based expectations

Testing async code is not simple, but XCTest provides us with all the required tool. This post shows how to wait for an expectation to be fulfilled based on a Swift closure.

How to use dependency injection for classes in Swift

In Swift it is possible to pass a reference to a type itself, not just to an instance of it. This post shows how to use this capability to test legacy code.

Implicitly vs Force Unwrapping Swift Optionals

A look at what implicitly unwrapping and force unwrap a Swift Optional mean, and how they differ from each other.

Why Implicitly Unwrapping Swift Optionals Is Dangerous

A look at what implicitly unwrapping an Optional value means and why it should be avoided.

Swift Either enum

This post introduces the Either type and shows a practical application of it in Swift, injecting extra cells in a table view.

Writing your own Swift "if let"

An exercise to understand Swift's optional type: reimplementing the if let functionality

What is an optional value in Swift

This post looks into one of Swift's most powerful feature: optionals

How to make Swift methods unavailable

A quick post showing how to use the Swift availability attribute to mark objects and functions as unavailable.

Functional Core Reactive Shell

This is a blogpost version of the content of my talk "Functional Core, Reactive Shell"

NSDateFormatter format for JSON dates

How to configure NSDateFormatter to work with JSON API dates.

Using Swift protocols to abstract third party dependencies and improve testability

Third party code can be hard to test, but you can use Swift's protocols to abstract its details and improve testability

Async Testing with Quick and Nimble

A look at how to write tests for async code when using the Quick and Nimble Swift frameworks. This post is part of the Practical Testing in Swift series.

Testing Delegates in Swift with XCTest

In this second post of the Practical Testing in Swift we a look at strategies to test how objects call their delegate methods or set property on them.

Testing callbacks in Swift with XCTest

Prevent Unit Tests from Loading AppDelegate in Swift

How to prevent the unit test target from loading the AppDelegate and have faster tests execution.

Fixing Bugs Driven By Tests in Swift

Unit and acceptance test are powerful tools that can be used to identify and fix bugs. Let's see how using a bugged Swift app as an example.

When to use map, flatMap, or for loops in Swift

Swift allows us to natively iterate over arrays using map. Map could be used to replace every for loop in your code, but that's not a great idea. Map and for have different purposes and should be used appropriately

Swift Optionals, Functional Programming, and You

This is the post version of a talk I've been given in the past months. In this post we will demystify functional programming terms like monad and functor, and see how those concepts can be brought back to the every day Swift development, in particular how they can help to deal with optionals in a leaner way.

Enhancing XCTest test cases with Nimble matchers

Nimble is a matchers framework built for Swift that provides powerful and versatile expectations. Writing test within the standard XCTest harness but using Nimble assertions is easier and productive, and a good combination of tools to introduce testing and TDD to colleagues and teams in a frictionless way.

Explicit Dependencies, Swift Edition

A look at how to write classes and structs that expose their dependencies as initialization arguments in Swift.

Swift array of characters from String

How to get an array of single characters String from a multiple characters String. From foobar to [f, o, o, b, a, r].

Packaging an ipa with Swift files from the terminal

If you are having problems with xcodebuild failing to export your apps with either Swift or Watch Kit support here's the solution, with a handy custom script.

Swift Functors, Applicatives, and Monads in Pictures

In this port to Swift of the great of Haskell's "Functors, Applicatives, And Monads In Pictures" we are going to look at these functional programming concepts aided by some very helpful pictures.