Post

Replies

Boosts

Views

Activity

How to mock a protocol with generics
I have the following protocol that a NetworkEngine conforms to: protocol NetworkEngineProtocol { func request<T: Decodable>(from endpoint: Endpoint) async throws -> T } This protocol is passed as a dependency into a custom ViewModel and I want to unit test the request method of the ViewModel, that itself calls the dependency request method. class ViewModel: ViewModelProtocol { var passthrough: PassthroughSubject<[SomeProperty], Never> = PassthroughSubject<[SomeProperty], Never>() private let networkEngine: NetworkEngineProtocol init(networkEngine: NetworkEngineProtocol) { self.networkEngine = networkEngine } func request() { Task { do { let model = try await networkEngine.request(from: .messages()) as CustomModel // ... do more stuff passthrough.send(someValue) // would want to test this } catch let err as NetworkEngineErrors { // ... error handling } } } } struct CustomModel: Decodable { let message: [String] let status: String } In order to Unit Test this method, I'd have to create a Mock class, that conforms to the NetworkEngineProtocol. Something like: class MockNetworkProtocol: NetworkEngineProtocol { init() {} func request<T>(from endpoint: Endpoint) async throws -> T where T : Decodable { // how to mock this? } } What's the best way to do this? How can I mock the request method of the NetworkEngine? Thanks
1
0
1.5k
May ’22
Bar button item custom view not detecting UITapGesture
I am encountering some weird behavior, where a subview within a UIBarButtonItem.customView is not firing the tapGesture. private var leftBarButtonItem: UIBarButtonItem = { let b = UIBarButtonItem() return b }() private lazy var imageView: UIImageView = { let iv = UIImageView() //... some adicional setup iv.isUserInteractionEnabled = true return iv }() func setupBBItem() { let holderView = UIView() holderView.isUserInteractionEnabled = true holderView.addSubview(imageView) imageView.frame = //... some frame //... more setups here holderView.isUserInteractionEnabled = true leftBarButtonItem.customView = holderView navigationItem.leftBarButtonItem = leftBarButtonItem let gesture = UITapGestureRecognizer(target: self, action: #selector(someFunction)) imageView.addGestureRecognizer(gesture) } @objc private func someFunction() { 	print("tap gesture detected") } I have even tried to add the gesture to the holderView instead, but the result is the same, the function never fires. Am I missing something here?
3
0
2.2k
Sep ’20