Comments (8)
Thanks. Can you be more specific and come up with a use case where you would want to mock closure. Till now Mockit only supports mocking classes.
from mockit.
it is a little bit complex example using RxSwift and Alamofire Router. this method will be called passing a closure which will be called when this method get token first from backend.
in my test case, i want to mock a closure and pass it to this method and verify that closure(token) is called once
func getResponseFromKombi<T>(closure: @escaping (String) -> URLRequestConvertible) -> Observable<T>{
return getTokenIfNotExist().flatMap { token in
return self.createObservable(closure(token)).debug("my probe")
}
}
from mockit.
I see. If we intend to mock it, then what that means is that we want to verify the action gets executed. But that can't be mocked. So instead interface-based approach should be used.
from mockit.
I already did this as a workaround. I created protocol with one method instead of closure.
but I submitted this ticket as it is better to have this feature in future.
By the way, I had many issues with Mockit even after using protocol. the method I want to mock should return Alamofire Router (URLRequestConvertible) which I failed to do after reading all example implementations. Any way, this is another issue I dont know if it is related to Mockit or my implementation. For now, I used traditional way by creating normal class with counter variable. I skipped Mockit for now as it didnt help me in my case. Again, it may be I who didnt understand Mockit well. I will give it another try in future
from mockit.
open class UIAlertControllerMock: UIAlertControllerImpl, Mock {
open let callHandler: CallHandler
public required init() {
callHandler = CallHandlerImpl(withTestCase: XCTestCase(invocation: nil))
}
public init(testCase: XCTestCase) {
callHandler = CallHandlerImpl(withTestCase: testCase)
}
open func instanceType() -> UIAlertControllerMock {
return self
}
open override func actualUIAlertController() -> UIAlertController {
return callHandler.accept(super.actualUIAlertController(), ofFunction: #function, atFile: #file, inLine: #line) as! UIAlertController
}
}
Look at the last method where I return the actualAlertController instance. Your mock should extend the Implementation class and obtain the router instance from there and pass it here. So it can be something like
open override func getRouter() -> URLRequestConvertible {
return callHandler.accept(super.actualRouter(), ofFunction: #function, atFile: #file, inLine: #line) as! URLRequestConvertible
}
Hope it helps :)
from mockit.
Many thanks. I will give it a try
from mockit.
Sure no problem. Any new feature or improvement recommendation is highly appreciable.
from mockit.
Hello,
May I have your email please so I can communicate with you. I need to know few things from you.
Have a good day.
from mockit.
Related Issues (13)
- Add Manual Installation
- Issue with testing func with a closure parameter HOT 1
- Does it work at Swift 3.2 and/or Swift 4.0 ? HOT 5
- Static mocking? HOT 1
- How to stub the same method twice with different arguments HOT 2
- When is Swift 5 migration planned? HOT 1
- 1.5.0 version unavailable on CocoaPods HOT 1
- Could you update the Swift version?
- OSX Compatibility
- Async HOT 1
- Please release v1.3.3 HOT 2
- Mocking an subclass of ManagedObject HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockit.