Skip to content

Allow passing an array of values to stub#551

Closed
jdanthinne wants to merge 2 commits intoBrightify:masterfrom
jdanthinne:master
Closed

Allow passing an array of values to stub#551
jdanthinne wants to merge 2 commits intoBrightify:masterfrom
jdanthinne:master

Conversation

@jdanthinne
Copy link
Contributor

As spreading an array doesn't work in Swift, I've added an extension to be able to pass an array of returned values to a stub.

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.

@MatyasKriz
Copy link
Collaborator

Oh, I forgot to merge this. Merging. 🙂

@MatyasKriz MatyasKriz closed this Jun 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants