Skip to content

Commit 9655f00

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 9655f00

File tree

11 files changed

+133
-87
lines changed

11 files changed

+133
-87
lines changed

Samples/TicTacToe/Tests/AuthenticationWorkflowTests.swift

+10-14
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,11 @@ class AuthenticationWorkflowTests: XCTestCase {
218218
onLoginTapped: {}
219219
)
220220
)
221-
.expect(
222-
worker: AuthenticationWorkflow.AuthorizingEmailPasswordWorker(
223-
authenticationService: authenticationService,
224-
225-
password: "password"
226-
)
227-
)
221+
.expectWorker(AuthenticationWorkflow.AuthorizingEmailPasswordWorker(
222+
authenticationService: authenticationService,
223+
224+
password: "password"
225+
))
228226
.render { screen in
229227
XCTAssertNil(screen.alert)
230228
}
@@ -252,13 +250,11 @@ class AuthenticationWorkflowTests: XCTestCase {
252250
onLoginTapped: {}
253251
)
254252
)
255-
.expect(
256-
worker: AuthenticationWorkflow.AuthorizingTwoFactorWorker(
257-
authenticationService: authenticationService,
258-
intermediateToken: "intermediateSession",
259-
twoFactorCode: "twoFactorCode"
260-
)
261-
)
253+
.expectWorker(AuthenticationWorkflow.AuthorizingTwoFactorWorker(
254+
authenticationService: authenticationService,
255+
intermediateToken: "intermediateSession",
256+
twoFactorCode: "twoFactorCode"
257+
))
262258
.render { screen in
263259
XCTAssertNil(screen.alert)
264260
}

Samples/Tutorial/Tutorial5.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ workflow
334334
producingRendering: ChildScreen(),
335335
producingOutput: .closed
336336
)
337-
.expect(
338-
worker: TestWorker(),
339-
producingOutput: .finished
340-
)
337+
.expectWorker(TestWorker(), mockingOutput: .finished)
341338
.render { rendering in
342339
XCTAssertEqual("expected text on rendering", rendering.text)
343340
}

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: "expectWorker", message: "Renamed")
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+
expectWorker(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 expectWorker<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

+4-11
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+
.expectWorker(TestWorker(input: "otherText"))
3131
.render { _ in }
3232
}
3333

@@ -36,10 +36,7 @@ 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"
42-
)
39+
.expectWorker(TestWorker(input: "otherText"), mockingOutput: "otherText")
4340
.render { _ in }
4441
.verifyState { state in
4542
XCTAssertEqual(state, TestWorkflow.State(mode: .worker(input: "otherText"), output: "otherText"))
@@ -49,9 +46,7 @@ class WorkflowCombineTestingTests: XCTestCase {
4946
func test_worker_missing() {
5047
let tester = TestWorkflow()
5148
.renderTester()
52-
.expect(
53-
worker: TestWorker(input: "input")
54-
)
49+
.expectWorker(TestWorker(input: "input"))
5550

5651
expectingFailure(#"Expected child workflow of type: WorkerWorkflow<TestWorker>, key: """#) {
5752
tester.render { _ in }
@@ -61,9 +56,7 @@ class WorkflowCombineTestingTests: XCTestCase {
6156
func test_worker_mismatch() {
6257
let tester = TestWorkflow()
6358
.renderTester(initialState: .init(mode: .worker(input: "test"), output: ""))
64-
.expect(
65-
worker: TestWorker(input: "not-test")
66-
)
59+
.expectWorker(TestWorker(input: "not-test"))
6760

6861
expectingFailures([
6962
#"Workers of type TestWorker not equivalent. Expected: TestWorker(input: "not-test"). Got: TestWorker(input: "test")"#,

WorkflowConcurrency/Testing/WorkerTesting.swift

+23-3
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,37 @@ 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: "expectWorker", message: "Renamed")
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+
expect(worker: 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 `worker`.
43+
///
44+
/// - Parameters:
45+
/// - worker: 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 expectWorker<ExpectedWorkerType: Worker>(
51+
_ worker: 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,

WorkflowConcurrency/TestingTests/TestingTests.swift

+4-11
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+
.expectWorker(TestWorker(input: "otherText"))
3030
.render { _ in }
3131
}
3232

@@ -35,10 +35,7 @@ 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"
41-
)
38+
.expectWorker(TestWorker(input: "otherText"), mockingOutput: "otherText")
4239
.render { _ in }
4340
.verifyState { state in
4441
XCTAssertEqual(state, TestWorkflow.State(mode: .worker(input: "otherText"), output: "otherText"))
@@ -48,9 +45,7 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
4845
func test_worker_missing() {
4946
let tester = TestWorkflow()
5047
.renderTester()
51-
.expect(
52-
worker: TestWorker(input: "input")
53-
)
48+
.expectWorker(TestWorker(input: "input"))
5449

5550
expectingFailure(#"Expected child workflow of type: WorkerWorkflow<TestWorker>, key: """#) {
5651
tester.render { _ in }
@@ -60,9 +55,7 @@ class WorkflowConcurrencyTestingTests: XCTestCase {
6055
func test_worker_mismatch() {
6156
let tester = TestWorkflow()
6257
.renderTester(initialState: .init(mode: .worker(input: "test"), output: ""))
63-
.expect(
64-
worker: TestWorker(input: "not-test")
65-
)
58+
.expectWorker(TestWorker(input: "not-test"))
6659

6760
expectingFailures([
6861
#"Workers of type TestWorker not equivalent. Expected: TestWorker(input: "not-test"). Got: TestWorker(input: "test")"#,

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: "expectWorker", message: "Renamed")
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+
expectWorker(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 expectWorker<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

+4-11
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+
.expectWorker(TestWorker(input: "otherText"))
3131
.render { _ in }
3232
}
3333

@@ -36,10 +36,7 @@ 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"
42-
)
39+
.expectWorker(TestWorker(input: "otherText"), mockingOutput: "otherText")
4340
.render { _ in }
4441
.verifyState { state in
4542
XCTAssertEqual(state, TestWorkflow.State(mode: .worker(input: "otherText"), output: "otherText"))
@@ -49,9 +46,7 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
4946
func test_worker_missing() {
5047
let tester = TestWorkflow()
5148
.renderTester()
52-
.expect(
53-
worker: TestWorker(input: "input")
54-
)
49+
.expectWorker(TestWorker(input: "input"))
5550

5651
expectingFailure(#"Expected child workflow of type: WorkerWorkflow<TestWorker>, key: """#) {
5752
tester.render { _ in }
@@ -61,9 +56,7 @@ class WorkflowReactiveSwiftTestingTests: XCTestCase {
6156
func test_worker_mismatch() {
6257
let tester = TestWorkflow()
6358
.renderTester(initialState: .init(mode: .worker(input: "test"), output: ""))
64-
.expect(
65-
worker: TestWorker(input: "not-test")
66-
)
59+
.expectWorker(TestWorker(input: "not-test"))
6760

6861
expectingFailures([
6962
#"Workers of type TestWorker not equivalent. Expected: TestWorker(input: "not-test"). Got: TestWorker(input: "test")"#,

WorkflowRxSwift/Testing/WorkerTesting.swift

+27-6
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,49 @@ import XCTest
2121
@testable import WorkflowRxSwift
2222

2323
extension RenderTester {
24-
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.
25-
24+
/// Mock the given worker's output, and assert that the workflow's worker `isEquivalent(to:)` the `worker`.
25+
///
2626
/// - Parameters:
27-
/// - worker: The worker to be expected
28-
/// - output: 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.
31+
/// - key: Key to expect this `Workflow` to be rendered with.
32+
@available(*, deprecated, renamed: "expectWorker", message: "Renamed")
2933
public func expect<ExpectedWorkerType: Worker>(
3034
worker: ExpectedWorkerType,
3135
producingOutput output: ExpectedWorkerType.Output? = nil,
3236
key: String = "",
3337
file: StaticString = #file, line: UInt = #line
38+
) -> RenderTester<WorkflowType> {
39+
expectWorker(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 expectWorker<ExpectedWorkerType: Worker>(
51+
_ expectedWorker: ExpectedWorkerType,
52+
mockingOutput output: ExpectedWorkerType.Output? = nil,
53+
key: String = "",
54+
file: StaticString = #file, line: UInt = #line
3455
) -> RenderTester<WorkflowType> {
3556
expectWorkflow(
3657
type: WorkerWorkflow<ExpectedWorkerType>.self,
3758
key: key,
3859
producingRendering: (),
3960
producingOutput: output,
4061
assertions: { workflow in
41-
guard !workflow.worker.isEquivalent(to: worker) else {
62+
guard !workflow.worker.isEquivalent(to: expectedWorker) else {
4263
return
4364
}
4465
XCTFail(
45-
"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)",
4667
file: file,
4768
line: line
4869
)

0 commit comments

Comments
 (0)