Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate async mocks #54

Open
alschmut opened this issue Dec 29, 2021 · 6 comments
Open

Generate async mocks #54

alschmut opened this issue Dec 29, 2021 · 6 comments

Comments

@alschmut
Copy link

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
    }
}
@Allje
Copy link

Allje commented Mar 31, 2022

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

@pkurzok
Copy link

pkurzok commented Mar 31, 2022

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

@PatrickGaissert
Copy link

We also need this in our project.

@MatiasGinart
Copy link

I am here just to say that we also need this

@jowie
Copy link

jowie commented Nov 7, 2022

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.

@beyzaince
Copy link

beyzaince commented Feb 18, 2023

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

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

No branches or pull requests

7 participants