You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Spyable is a powerful tool for Swift that simplifies and automates the process of creating spies for testing. By using
9
-
the `@Spyable` annotation on a protocol, the macro generates a spy class that implements the same interface and tracks
10
-
interactions with its methods and properties.
8
+
Spyable is a powerful tool for Swift that automates the process of creating protocol-conforming classes. Initially designed to simplify testing by generating spies, it is now widely used for various scenarios, such as SwiftUI previews or creating quick dummy implementations.
11
9
12
10
## Overview
13
11
14
-
A "spy" is a test double that replaces a real component and records all interactions for later inspection. It's
15
-
particularly useful in behavior verification, where the interaction between objects is the subject of the test.
12
+
Spyable enhances your Swift workflow with the following features:
16
13
17
-
The Spyable macro revolutionizes the process of creating spies in Swift testing:
18
-
19
-
-**Automatic Spy Generation**: Annotate a protocol with `@Spyable`, and let the macro generate the corresponding spy class.
20
-
-**Interaction Tracking**: The generated spy records method calls, arguments, and return values, making it easy to verify behavior in your tests.
14
+
-**Automatic Spy Generation**: Annotate a protocol with `@Spyable`, and let the macro generate a corresponding spy class.
15
+
-**Access Level Inheritance**: The generated class automatically inherits the protocol's access level.
16
+
-**Explicit Access Control**: Use the `accessLevel` argument to override the inherited access level if needed.
17
+
-**Interaction Tracking**: For testing, the generated spy tracks method calls, arguments, and return values.
21
18
22
19
## Quick Start
23
20
@@ -26,33 +23,33 @@ The Spyable macro revolutionizes the process of creating spies in Swift testing:
This generates a spy class named `ServiceProtocolSpy` that implements `ServiceProtocol`. The generated class includes
36
-
properties and methods for tracking method calls, arguments, and return values.
32
+
This generates a spy class named `ServiceProtocolSpy` with a `public` access level. The generated class includes properties and methods for tracking method calls, arguments, and return values.
37
33
38
34
```swift
39
-
classServiceProtocolSpy: ServiceProtocol {
40
-
var name: String {
35
+
publicclassServiceProtocolSpy: ServiceProtocol {
36
+
publicvar name: String {
41
37
get { underlyingName }
42
38
set { underlyingName = newValue }
43
39
}
44
-
var underlyingName: (String)!
40
+
publicvar underlyingName: (String)!
45
41
46
-
var fetchConfigArgCallsCount =0
47
-
var fetchConfigArgCalled: Bool {
42
+
publicvar fetchConfigArgCallsCount =0
43
+
publicvar fetchConfigArgCalled: Bool {
48
44
return fetchConfigArgCallsCount >0
49
45
}
50
-
var fetchConfigArgReceivedArg: UInt8?
51
-
var fetchConfigArgReceivedInvocations: [UInt8] = []
52
-
var fetchConfigArgThrowableError: (anyError)?
53
-
var fetchConfigArgReturnValue: [String: String]!
54
-
var fetchConfigArgClosure: ((UInt8) asyncthrows-> [String: String])?
Spyable supports generic functions, but their implementation involves special handling. Due to limitations in Swift, generic parameters in a function are replaced with `Any` in the spy class to store arguments, return values, and closures.
97
-
98
-
For example:
93
+
By default, the generated spy inherits the access level of the annotated protocol. For example:
0 commit comments