Skip to content

Generate async mocks #54

Description

@alschmut

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
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions