SwiftMockGeneratorForXcode icon indicating copy to clipboard operation
SwiftMockGeneratorForXcode copied to clipboard

Generate async mocks

Open alschmut opened this issue 3 years ago • 6 comments

As a developer I want get correctly generated mocks when using the new async keyword from the concurrency feature introduced with Swift 5.5.

Using the Version 0.27 (3) I experienced the following issue: When mocking an async function, then...

  • the async keyword is not added to the mocked function
  • the return value is ignored
  • the throws keyword is ignored
// protocol
func myAsync() async

// current mock
var invokedMyAsync = false
var invokedMyAsyncCount = 0

func myAsync() {
    invokedMyAsync = true
    invokedMyAsyncCount += 1
}

// expected mock
var invokedMyAsync = false
var invokedMyAsyncCount = 0

func myAsync() async {
    invokedMyAsync = true
    invokedMyAsyncCount += 1
}
// protocol
func myAsyncReturns() async -> MyObject

// current mock
var invokedMyAsyncReturns = false
var invokedMyAsyncReturnsCount = 0

func myAsyncReturns() {
    invokedMyAsyncReturns = true
    invokedMyAsyncReturnsCount += 1
}

// expected mock
var invokedMyAsyncReturns = false
var invokedMyAsyncReturnsCount = 0
var stubbedMyAsyncReturnsResult: MyObject!

func myAsyncReturns() async -> MyObject {
    invokedMyAsyncReturns = true
    invokedMyAsyncReturnsCount += 1
    return stubbedMyAsyncReturnsResult
}
// protocol
func myAsyncThrows() async throws

// current mock
var invokedMyAsyncThrows = false
var invokedMyAsyncThrowsCount = 0

func myAsyncThrows() {
    invokedMyAsyncThrows = true
    invokedMyAsyncThrowsCount += 1
}

// expected mock
var invokedMyAsyncThrows = false
var invokedMyAsyncThrowsCount = 0
var invokedMyAsyncThrowsError: Error?

func myAsyncThrows() async throws {
    invokedMyAsyncThrows = true
    invokedMyAsyncThrowsCount += 1
    if let error = invokedMyAsyncThrowsError {
        throw error
    }
}

alschmut avatar Dec 29 '21 09:12 alschmut

I have experienced the same problem. Is there any solution to this?

Allje avatar Mar 31 '22 06:03 Allje

I could use this too in my current project. Please make it happen!

pkurzok avatar Mar 31 '22 12:03 pkurzok

We also need this in our project.

PatrickGaissert avatar Jun 21 '22 12:06 PatrickGaissert

I am here just to say that we also need this

MatiasGinart avatar Oct 24 '22 18:10 MatiasGinart

In the meantime, a tip: Copy the protocol into the mock file, remove the keyword async from all function names and then build the mock. Then you can add it back afterwards. The throws functions will no longer be ignored.

jowie avatar Nov 07 '22 16:11 jowie

We need this feature in our project. Are there any updates here?

beyzaince avatar Feb 18 '23 13:02 beyzaince