Skip to content

Commit a5f5592

Browse files
committed
Rename expect(worker:producingOutput:) to be more clear
When I first saw tests that used this function, I thought it was asserting that the `worker` passed in, matches the `workflow.worker` (which it does do), but then I thought it was asserting that `producingOutput == workflow.worker.output` (which it does NOT do). Instead, neither of the workers are ever run. The `producingOutput` is used as mock output. So renaming these to make it more clear what is going on.
1 parent c9b9d99 commit a5f5592

File tree

12 files changed

+150
-69
lines changed

12 files changed

+150
-69
lines changed

MigrationGuide_v1.0.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ NameLoadingWorkflow()
120120
// After
121121
NameLoadingWorkflow()
122122
.renderTester(initialState: .init(state: .loading, token: "user-token"))
123-
.expect(
124-
worker: LoadingWorker(token: "user-token"),
125-
producingOutput: .success("Ben Cochran")
123+
.mockWorker(
124+
expectedWorker: LoadingWorker(token: "user-token"),
125+
mockingOutput: .success("Ben Cochran")
126126
)
127127
.render { rendering in
128128
XCTAssertEqual(rendering.title, "Loading")

Samples/TicTacToe/Tests/AuthenticationWorkflowTests.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ class AuthenticationWorkflowTests: XCTestCase {
218218
onLoginTapped: {}
219219
)
220220
)
221-
.expect(
222-
worker: AuthenticationWorkflow.AuthorizingEmailPasswordWorker(
221+
.mockWorker(
222+
expectedWorker: AuthenticationWorkflow.AuthorizingEmailPasswordWorker(
223223
authenticationService: authenticationService,
224224
225225
password: "password"
@@ -252,8 +252,8 @@ class AuthenticationWorkflowTests: XCTestCase {
252252
onLoginTapped: {}
253253
)
254254
)
255-
.expect(
256-
worker: AuthenticationWorkflow.AuthorizingTwoFactorWorker(
255+
.mockWorker(
256+
expectedWorker: AuthenticationWorkflow.AuthorizingTwoFactorWorker(
257257
authenticationService: authenticationService,
258258
intermediateToken: "intermediateSession",
259259
twoFactorCode: "twoFactorCode"

Samples/Tutorial/Tutorial5.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ workflow
334334
producingRendering: ChildScreen(),
335335
producingOutput: .closed
336336
)
337-
.expect(
338-
worker: TestWorker(),
339-
producingOutput: .finished
337+
.mockWorker(
338+
expectedWorker: TestWorker(),
339+
mockingOutput: .finished
340340
)
341341
.render { rendering in
342342
XCTAssertEqual("expected text on rendering", rendering.text)

WorkflowCombine/Testing/WorkerTesting.swift

+25-5
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,49 @@ import XCTest
2121
@testable import WorkflowCombine
2222

2323
extension RenderTester {
24-
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.
24+
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `worker`.
2525
///
2626
/// - Parameters:
27-
/// - worker: The worker to be expected
28-
/// - producingOutput: An output that will be returned when this worker is requested, if any.
27+
/// - worker: The worker that we expect was created by the workflow. Will be compared using
28+
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
29+
/// - producingOutput: The output to be used instead of actually running the worker.
30+
/// If the workflow never tries to run the worker, then this won't be used.
2931
/// - key: Key to expect this `Workflow` to be rendered with.
32+
@available(*, deprecated, renamed: "mockWorker", message: "This method's name & parameter names were unclear. It has been renamed to mockWorker.")
3033
public func expect<ExpectedWorkerType: Worker>(
3134
worker: ExpectedWorkerType,
3235
producingOutput output: ExpectedWorkerType.Output? = nil,
3336
key: String = "",
3437
file: StaticString = #file, line: UInt = #line
38+
) -> RenderTester<WorkflowType> {
39+
mockWorker(expectedWorker: worker, mockingOutput: output, key: key, file: file, line: line)
40+
}
41+
42+
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `expectedWorker`.
43+
///
44+
/// - Parameters:
45+
/// - expectedWorker: The worker that we expect was created by the workflow. Will be compared using
46+
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
47+
/// - mockingOutput: The output to be used instead of actually running the worker.
48+
/// If the workflow never tries to run the worker, then this won't be used.
49+
/// - key: Key to expect this `Workflow` to be rendered with.
50+
public func mockWorker<ExpectedWorkerType: Worker>(
51+
expectedWorker: ExpectedWorkerType,
52+
mockingOutput output: ExpectedWorkerType.Output? = nil,
53+
key: String = "",
54+
file: StaticString = #file, line: UInt = #line
3555
) -> RenderTester<WorkflowType> {
3656
expectWorkflow(
3757
type: WorkerWorkflow<ExpectedWorkerType>.self,
3858
key: key,
3959
producingRendering: (),
4060
producingOutput: output,
4161
assertions: { workflow in
42-
guard !workflow.worker.isEquivalent(to: worker) else {
62+
guard !workflow.worker.isEquivalent(to: expectedWorker) else {
4363
return
4464
}
4565
XCTFail(
46-
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(worker). Got: \(workflow.worker)",
66+
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(expectedWorker). Got: \(workflow.worker)",
4767
file: file,
4868
line: line
4969
)

WorkflowCombine/TestingTests/TestingTests.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class WorkflowCombineTestingTests: XCTestCase {
2727
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))
2828

2929
renderTester
30-
.expect(worker: TestWorker(input: "otherText"))
30+
.mockWorker(expectedWorker: TestWorker(input: "otherText"))
3131
.render { _ in }
3232
}
3333

@@ -36,9 +36,9 @@ class WorkflowCombineTestingTests: XCTestCase {
3636
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))
3737

3838
renderTester
39-
.expect(
40-
worker: TestWorker(input: "otherText"),
41-
producingOutput: "otherText"
39+
.mockWorker(
40+
expectedWorker: TestWorker(input: "otherText"),
41+
mockingOutput: "otherText"
4242
)
4343
.render { _ in }
4444
.verifyState { state in
@@ -49,8 +49,8 @@ class WorkflowCombineTestingTests: XCTestCase {
4949
func test_worker_missing() {
5050
let tester = TestWorkflow()
5151
.renderTester()
52-
.expect(
53-
worker: TestWorker(input: "input")
52+
.mockWorker(
53+
expectedWorker: TestWorker(input: "input")
5454
)
5555

5656
expectingFailure(#"Expected child workflow of type: WorkerWorkflow<TestWorker>, key: """#) {
@@ -61,8 +61,8 @@ class WorkflowCombineTestingTests: XCTestCase {
6161
func test_worker_mismatch() {
6262
let tester = TestWorkflow()
6363
.renderTester(initialState: .init(mode: .worker(input: "test"), output: ""))
64-
.expect(
65-
worker: TestWorker(input: "not-test")
64+
.mockWorker(
65+
expectedWorker: TestWorker(input: "not-test")
6666
)
6767

6868
expectingFailures([

WorkflowConcurrency/Testing/WorkerTesting.swift

+25-5
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,49 @@ import XCTest
2121
@testable import WorkflowConcurrency
2222

2323
extension RenderTester {
24-
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.
24+
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `worker`.
2525
///
2626
/// - Parameters:
27-
/// - worker: The worker to be expected
28-
/// - producingOutput: An output that will be returned when this worker is requested, if any.
27+
/// - worker: The worker that we expect was created by the workflow. Will be compared using
28+
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
29+
/// - producingOutput: The output to be used instead of actually running the worker.
30+
/// If the workflow never tries to run the worker, then this won't be used.
2931
/// - key: Key to expect this `Workflow` to be rendered with.
32+
@available(*, deprecated, renamed: "mockWorker", message: "This method's name & parameter names were unclear. It has been renamed to mockWorker.")
3033
public func expect<ExpectedWorkerType: Worker>(
3134
worker: ExpectedWorkerType,
3235
producingOutput output: ExpectedWorkerType.Output? = nil,
3336
key: String = "",
3437
file: StaticString = #file, line: UInt = #line
38+
) -> RenderTester<WorkflowType> {
39+
mockWorker(expectedWorker: worker, mockingOutput: output, key: key, file: file, line: line)
40+
}
41+
42+
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `expectedWorker`.
43+
///
44+
/// - Parameters:
45+
/// - expectedWorker: The worker that we expect was created by the workflow. Will be compared using
46+
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
47+
/// - mockingOutput: The output to be used instead of actually running the worker.
48+
/// If the workflow never tries to run the worker, then this won't be used.
49+
/// - key: Key to expect this `Workflow` to be rendered with.
50+
public func mockWorker<ExpectedWorkerType: Worker>(
51+
expectedWorker: ExpectedWorkerType,
52+
mockingOutput output: ExpectedWorkerType.Output? = nil,
53+
key: String = "",
54+
file: StaticString = #file, line: UInt = #line
3555
) -> RenderTester<WorkflowType> {
3656
expectWorkflow(
3757
type: WorkerWorkflow<ExpectedWorkerType>.self,
3858
key: key,
3959
producingRendering: (),
4060
producingOutput: output,
4161
assertions: { workflow in
42-
guard !workflow.worker.isEquivalent(to: worker) else {
62+
guard !workflow.worker.isEquivalent(to: expectedWorker) else {
4363
return
4464
}
4565
XCTFail(
46-
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(worker). Got: \(workflow.worker)",
66+
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(expectedWorker). Got: \(workflow.worker)",
4767
file: file,
4868
line: line
4969
)

WorkflowConcurrency/TestingTests/TestingTests.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
2626
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))
2727

2828
renderTester
29-
.expect(worker: TestWorker(input: "otherText"))
29+
.mockWorker(expectedWorker: TestWorker(input: "otherText"))
3030
.render { _ in }
3131
}
3232

@@ -35,9 +35,9 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
3535
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))
3636

3737
renderTester
38-
.expect(
39-
worker: TestWorker(input: "otherText"),
40-
producingOutput: "otherText"
38+
.mockWorker(
39+
expectedWorker: TestWorker(input: "otherText"),
40+
mockingOutput: "otherText"
4141
)
4242
.render { _ in }
4343
.verifyState { state in
@@ -48,8 +48,8 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
4848
func test_worker_missing() {
4949
let tester = TestWorkflow()
5050
.renderTester()
51-
.expect(
52-
worker: TestWorker(input: "input")
51+
.mockWorker(
52+
expectedWorker: TestWorker(input: "input")
5353
)
5454

5555
expectingFailure(#"Expected child workflow of type: WorkerWorkflow<TestWorker>, key: """#) {
@@ -60,8 +60,8 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
6060
func test_worker_mismatch() {
6161
let tester = TestWorkflow()
6262
.renderTester(initialState: .init(mode: .worker(input: "test"), output: ""))
63-
.expect(
64-
worker: TestWorker(input: "not-test")
63+
.mockWorker(
64+
expectedWorker: TestWorker(input: "not-test")
6565
)
6666

6767
expectingFailures([

WorkflowReactiveSwift/Testing/WorkerTesting.swift

+25-5
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,49 @@ import XCTest
2121
@testable import WorkflowReactiveSwift
2222

2323
extension RenderTester {
24-
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.
24+
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `worker`.
2525
///
2626
/// - Parameters:
27-
/// - worker: The worker to be expected
28-
/// - producingOutput: An output that will be returned when this worker is requested, if any.
27+
/// - worker: The worker that we expect was created by the workflow. Will be compared using
28+
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
29+
/// - producingOutput: The output to be used instead of actually running the worker.
30+
/// If the workflow never tries to run the worker, then this won't be used.
2931
/// - key: Key to expect this `Workflow` to be rendered with.
32+
@available(*, deprecated, renamed: "mockWorker", message: "This method's name & parameter names were unclear. It has been renamed to mockWorker.")
3033
public func expect<ExpectedWorkerType: Worker>(
3134
worker: ExpectedWorkerType,
3235
producingOutput output: ExpectedWorkerType.Output? = nil,
3336
key: String = "",
3437
file: StaticString = #file, line: UInt = #line
38+
) -> RenderTester<WorkflowType> {
39+
mockWorker(expectedWorker: worker, mockingOutput: output, key: key, file: file, line: line)
40+
}
41+
42+
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `expectedWorker`.
43+
///
44+
/// - Parameters:
45+
/// - expectedWorker: The worker that we expect was created by the workflow. Will be compared using
46+
/// `isEquivalent(to:)` to assert that the workflow's worker matches.
47+
/// - mockingOutput: The output to be used instead of actually running the worker.
48+
/// If the workflow never tries to run the worker, then this won't be used.
49+
/// - key: Key to expect this `Workflow` to be rendered with.
50+
public func mockWorker<ExpectedWorkerType: Worker>(
51+
expectedWorker: ExpectedWorkerType,
52+
mockingOutput output: ExpectedWorkerType.Output? = nil,
53+
key: String = "",
54+
file: StaticString = #file, line: UInt = #line
3555
) -> RenderTester<WorkflowType> {
3656
expectWorkflow(
3757
type: WorkerWorkflow<ExpectedWorkerType>.self,
3858
key: key,
3959
producingRendering: (),
4060
producingOutput: output,
4161
assertions: { workflow in
42-
guard !workflow.worker.isEquivalent(to: worker) else {
62+
guard !workflow.worker.isEquivalent(to: expectedWorker) else {
4363
return
4464
}
4565
XCTFail(
46-
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(worker). Got: \(workflow.worker)",
66+
"Workers of type \(ExpectedWorkerType.self) not equivalent. Expected: \(expectedWorker). Got: \(workflow.worker)",
4767
file: file,
4868
line: line
4969
)

WorkflowReactiveSwift/TestingTests/TestingTests.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
2727
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))
2828

2929
renderTester
30-
.expect(worker: TestWorker(input: "otherText"))
30+
.mockWorker(expectedWorker: TestWorker(input: "otherText"))
3131
.render { _ in }
3232
}
3333

@@ -36,9 +36,9 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
3636
.renderTester(initialState: .init(mode: .worker(input: "otherText"), output: ""))
3737

3838
renderTester
39-
.expect(
40-
worker: TestWorker(input: "otherText"),
41-
producingOutput: "otherText"
39+
.mockWorker(
40+
expectedWorker: TestWorker(input: "otherText"),
41+
mockingOutput: "otherText"
4242
)
4343
.render { _ in }
4444
.verifyState { state in
@@ -49,8 +49,8 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
4949
func test_worker_missing() {
5050
let tester = TestWorkflow()
5151
.renderTester()
52-
.expect(
53-
worker: TestWorker(input: "input")
52+
.mockWorker(
53+
expectedWorker: TestWorker(input: "input")
5454
)
5555

5656
expectingFailure(#"Expected child workflow of type: WorkerWorkflow<TestWorker>, key: """#) {
@@ -61,8 +61,8 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
6161
func test_worker_mismatch() {
6262
let tester = TestWorkflow()
6363
.renderTester(initialState: .init(mode: .worker(input: "test"), output: ""))
64-
.expect(
65-
worker: TestWorker(input: "not-test")
64+
.mockWorker(
65+
expectedWorker: TestWorker(input: "not-test")
6666
)
6767

6868
expectingFailures([

0 commit comments

Comments
 (0)