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

'Generic parameter 'T' could not be inferred' when stubbing protocol with generics #401

Open
seboslaw opened this issue Oct 2, 2021 · 0 comments

Comments

@seboslaw
Copy link

seboslaw commented Oct 2, 2021

Hey guys,

I'm facing problems when setting up stubbing for the following protocol:

protocol KeyValueStorageProtocol {
    func load<T>(_ key: String, type: T.Type, completion: @escaping ((T) -> Void), failure: @escaping ((Error) -> Void)) where T: Codable
}

I'm currently saving Date objects in that Storage, so my test setup is as follows:

let mock = MockKeyValueStorageProtocol()
stub(mock) { stub in
  when(stub.load(any(), type: any(), completion: anyClosure(), failure: anyClosure()))
  .then({
    $2(Date())
  })
}

However, I'm getting the following error for the when(stub... line:

Generic parameter 'T' could not be inferred

So I'm not sure if providing the type (Date) in the .then command is enough to make the when(stub... resolve the generic type? I'm also not sure, I'm using the block syntax correctly here? I'm trying to make calls to the load function return a date in the completion block.

Any help or hints would be highly appreciated!

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

1 participant