Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ public extension StubFunctionThenReturnTrait {
}
return self
}

@discardableResult
func thenReturn(_ output: [OutputType]) -> Self {
output.forEach { output in
stub.appendAction(.returnValue(output))
}
return self
}
}
12 changes: 12 additions & 0 deletions Tests/Swift/Stubbing/StubbingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ final class StubbingTest: XCTestCase {
XCTAssertEqual(mock.readOnlyProperty, "c")
}

func testReturningArray() {
let mock = MockTestedClass()
stub(mock) { mock in
when(mock.readOnlyProperty.get).thenReturn(["a", "b", "c"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this be ambiguous when the property is an array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ambiguous for the compiler?
If readOnlyProperty is an array, the to generic method will translate to func thenReturn(_ output: [T], _ outputs: [T]...) -> Self and func thenReturn(_ output: [[T]]) -> Self.

So, if you give it a "simple" array (one dimension), it will known that it is the first method. I you give it an array of arrays (two dimensions), it will know that it is the second one.

Would you prefer to add a name to the argument for the new method, to be clearer for users?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd add something like inOrder: to the new method to disambiguate passing array of individually returned values from passing array as a singular one. I can imagine a scenario where some complex tests only tell the developer the classic "The compiler is unable to type-check this expression in reasonable time." because they might not know about the second method which returns the values one by one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the inOrder: parameter name.

}

XCTAssertEqual(mock.readOnlyProperty, "a")
XCTAssertEqual(mock.readOnlyProperty, "b")
XCTAssertEqual(mock.readOnlyProperty, "c")
XCTAssertEqual(mock.readOnlyProperty, "c")
}

func testOverrideStubWithMoreGeneralParameterMatcher() {
let mock = MockTestedClass()
stub(mock) { mock in
Expand Down
Loading