Cuckoo icon indicating copy to clipboard operation
Cuckoo copied to clipboard

Cant mock Protocol extensions with default parameters.

Open RoryKelly opened this issue 3 years ago • 3 comments

We have an autogenerated Network Stack. Cuckoo cannot mock protocol extensions that have functions with default parameters.

protocol ApolloNetworkService {
    var apolloClient: ApolloClient { get }

    func allClashesFeedQueryWatch(cachePolicy: CachePolicy) -> AnyPublisher<AllClashesFeedQueryModel, Error>

}

extension ApolloNetworkService {
    func allClashesFeedQueryWatch( cachePolicy: CachePolicy = .returnCacheDataAndFetch ) -> AnyPublisher<AllClashesFeedQueryModel, Error> {
       // this function cannot be mocked. 
    }
}


func test() {
     let mockNetwork = MockApolloNetworkService()

     stub(networkService) { stub in
                    when(allClashesFeedQueryWatch(cachePolicy: any())).thenReturn(Empty().eraseToAnyPublisher())
      }
  
   //  This calls actual implementation 
   mockNetwork. allClashesFeedQueryWatch() 

   // this returns mock
   mockNetwork. allClashesFeedQueryWatch(cachePolicy: .fromCache) 
}

RoryKelly avatar Dec 23 '21 11:12 RoryKelly

An interesting observation, thanks for reporting it! We should definitely add this case to the tests and fix it.

MatyasKriz avatar Jan 24 '22 18:01 MatyasKriz

I have the same issue.

ppamorim avatar Oct 07 '22 04:10 ppamorim

Any update on this? I'm currently facing the same limitation.

stonko1994 avatar Jan 19 '23 08:01 stonko1994