I try to create a class implementation skeleton and a test for it. I have already had the function setResult and test testSetResult. Could I have something similar for the function setPublisher and testSetPublisher?
class FakeClass {
// ...
public func setResult() -> Result<[String: Any], Error> {
return .success([:])
}
public func setPublisher() -> any Publisher<[String, Any], Error> {
// what should I return? return PassthroughSubject()
}
}
class FakeClassTest: XCTestCase {
// ...
var fakeClass = DependencyInjectionFakeClass() // some dependency injection
func testSetResult() throws {
let result = fakeClass.setResult()
switch result {
case .success(let response):
XCTAsserture(response.isEmpty)
case .failure:
XCTFail()
}
// just a simple fake test
// How could I implement it or do we have sth similar with the function `testSetResult()`
func testSetPublisher() throws {
}
}
Related
Context
I want to wrap the Alamofire.upload into an observable and having info regarding the upload progress.
For that I have created a custom UploadElement that is an enum representing either the progress and the value or the result. So far I have:
enum UploadElement<Result> where Result: Codable {
case progress(Double)
case response(Result)
}
private func buildUploadRequest(url: URL, parts: [Data]) -> Observable<UploadRequest> {
let uploadRequest = manager.upload(
multipartFormData: { multipartFormData in /* build multipart */ },
to: url
)
return Observable.just(uploadRequest)
}
func upload<Result: Codable>(url: URL, parts: [Data]) -> Observable<UploadElement<Result>> {
buildUploadRequest(url: url, parts: parts)
.flatMap { request in
Observable<UploadElement<Result>>.create { observer in
request.response { response in
do {
observer.on(.next(.response(/* decode here */)))
observer.on(.completed)
} catch let error {
observer.on(.error(error))
}
}.uploadProgress { progress in
observer.on(.next(.progress(progress.fractionCompleted)))
}
.resume()
return Disposable.create { request.cancel() }
}
}
}
Now I would like to have an extension on an Observable<UploadEment<Result>> to have a nicer way to be notified.
Basically it would be:
service.upload(url: ..., parts: ...)
.progress { progress in /* */ }
.result { result in /* */ }
.subscribe()
.dispose(by: disposeBag)
To do that I tried:
extension ObservableType where Element == UploadElement<Resource> {
func progress(progressCompletion: #escaping (Double) -> Void) -> Self {
return self.do(onNext: { element in
switch element {
case .progress(let progress): progressCompletion(progress)
case .response: return
}
})
}
func result(resultCompletion: #escaping (Result) -> Void) -> Self {
return self.do(onNext: { element in
switch element {
case .response(let result): resultCompletion(result)
case .progress: return
}
})
}
}
I tried multiple variation of that but the errors that I get are:
Cannot find 'Result in scope'
Reference to generic type ... required argument
Is it possible to achieve something like that?
You just need to move the where clause from class scope down to function scope (shown below).
That said, I don't think breaking out of the monad like this in the middle of a stream is "a nicer way to be notified".
Better would be to break your Observable into two streams and subscribe to each of them:
extension ObservableType {
func progress<Resource>() -> Observable<Double> where Element == UploadElement<Resource> {
self.compactMap { element in
switch element {
case let .progress(progress):
return progress
case .response:
return nil
}
}
}
func result<Resource>() -> Observable<Resource> where Element == UploadElement<Resource> {
self.compactMap { element in
switch element {
case .progress:
return nil
case let .response(resource):
return resource
}
}
}
}
With the above you can now do something like this:
let response = service.upload(url: ..., parts: ...)
.share()
response
.progress()
.subscribe(onNext: { progress in /*...*/ })
.disposed(by: disposeBag)
response
.result()
.subscribe(onNext: { result in /*...*/ })
.dispose(by: disposeBag)
Now you don't have any empty subscribes.
I found something that is working:
extension ObservableType {
func progress<O: Codable>(progressCompletion: #escaping (Double) -> Void) -> Observable<UploadElement<O>> where Element == UploadElement<O> {
return self.do(onNext: { element in
if case .progress(let progress) = element {
progressCompletion(progress)
}
})
}
func response<O: Codable>(responseCompletion: #escaping (O) -> Void) -> Observable<UploadElement<O>> where Element == UploadElement<O> {
return self.do(onNext: { element in
if case .response(let response) = element {
responseCompletion(response)
}
})
}
}
Now I can use the "planned" api:
service.update(data: /* ... */)
.progress { progress in /* */ }
.response { result in /* */ }
.subscribe(
onError: { error in /* */ }
)
.dispose(by: disposeBag)
However as Daniel mentioned this might not be the "nicer way of being notified".
So i have this code where i'm trying to make an task handler for requests. But in some cases the request doesn't get an model in response and therefor i don't want it to process any data. Hard to explain, but code shown below:
class UserTask<T: Codable>: ExecuteProtocol {
let userType: UserRequests
init(userType: UserRequests) {
self.userType = userType
}
var request: URLRequest {
return userType.build
}
public func run(completion: #escaping ((Response<T, NAError>) ->())) {
executeRequest(request: request) { (response) in
switch response {
case .success(let data):
completion(NADecoder<T>.decode(data: data).model)
break
case .failure(let error):
completion(.failure(error))
break
}
}
}
}
class UserTask: ExecuteProtocol {
let userType: UserRequests
init(userType: UserRequests) {
self.userType = userType
}
var request: URLRequest {
return userType.build
}
public func run(completion: #escaping ((Response<Any?, NAError>) ->())) {
executeRequest(request: request) { (response) in
switch response {
case .success(let data):
completion(.success(nil))
break
case .failure(let error):
completion(.failure(error))
break
}
}
}
}
This of course say Invalid redeclaration of 'UserTask' But can i do this in any smooth way? I have tried making the Codable optional and then unwrapping it. But as i want to keep the type of it in Decodable purpose it doesn't seem to work.
Any suggestions?
There is no need to create multiple classes for same functionality. You simply need to make some changes to a single class to support both your use-cases.
Instead of adding generic <T> to the class UserTask, add it to method run(completion:), i.e.
class UserTask: ExecuteProtocol {
let userType: UserRequests
init(userType: UserRequests) {
self.userType = userType
}
var request: URLRequest {
return userType.build
}
public func run<T: Codable>(type: T.Type, completion: #escaping ((Response<T?, NAError>) ->())) {
//your code here...
}
}
Call it like,
task.run(type: YourType.self) { (response) in
//add your code here...
}
I have multiple classes that have code that calls a common network class to make a GET api call. Below is an example of one:
public typealias Api1Result = (Result<Api1Model>) -> Void
private var path = "the/path/api1"
public enum Api1ServiceError: String, Error {
case error = "Sorry, the api1 service returned something different than expected"
}
extension Api1Model {
public static func getApi1(networkClient: NetworkClient = networkClient, completion: #escaping Api1Result) {
networkClient.getPath(path) { result in
switch result {
case .success(let data):
do {
let api1Model = try JSONDecoder().decode(Api1Model.self, from: data)
completion(.success(api1Model))
} catch {
completion(.failure(Api1ServiceError.error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
Here is the Result enum if interested:
public enum Result<Value> {
case success(Value)
case failure(Error)
}
There are several other model classes, and the only difference is the actual model class being decoded (Api1Model in this case), as well as the completion typealias (Api1Result). It does the exact same thing across several others, just makes the call to the networkClient.getPath() method, checks for success/failure, and calls the completion closure.
Curious if there are any protocol experts out there who could assist in simplifying this and refactoring so I don't have the same boiler-plate code across multiple classes?
Use a protocol extension (untested)
protocol ApiModel {
associatedtype ApiType : Decodable = Self
static var path : String { get }
static func getApi1(networkClient: NetworkClient, completion: #escaping (Result<ApiType>) -> Void)
}
extension ApiModel where Self : Decodable {
static func getApi1(networkClient: NetworkClient, completion: #escaping (Result<ApiType>) -> Void) {
networkClient.getPath(path) { result in
switch result {
case .success(let data):
do {
let api1Model = try JSONDecoder().decode(ApiType.self, from: data)
completion(.success(api1Model))
} catch {
completion(.failure(Api1ServiceError.error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
Make all your classes conform to ApiModel and add the static path property. The type alias is going to be inferred.
I am using an external library in Swift so I cannot control the return statements. My understanding is that I should wrap these returns in promises in order to use PromiseKit. Is this correct?
Assuming so, I have working code as follows:
private func getChannelImage(for channel: TCHChannel, completion: #escaping (UIImage?, CAProfileError?) -> Void) {
if let members = channel.members {
members.members(completion: { (result, paginator) in
if result.isSuccessful() {
// ... do something
}
else {
completion(nil, CAProfileError.UnknownError)
}
})
}
}
This can be difficult to read. I am trying to simplify this using PromiseKit. First, I want to simplify members.members(completion: { (result, paginator) in to a promise that I can call with the firstly { ... } syntax. I thus try and do as follows:
private func asPromise(members: TCHMembers) -> Promise<TCHMemberPaginator> {
return Promise<TCHMemberPaginator> { fulfill, reject in
members.members(completion: { (result, paginator) in
if result.isSuccesful() {
fulfill(paginator)
} else {
reject()
}
})
}
}
But this approach does not work and I get "Unable to infer closure type in the current context". I'm trying to find a good example of this use case done online but am having trouble. Any thoughts on how to properly return promises?
Assuming the TCHMemberPaginator and TCHMembers as below,
class TCHMemberPaginator {}
class TCHMembers {
func members(completion: (Bool, TCHMemberPaginator?) -> Void) {}
}
Here is the method to return a Promise,
private func asPromise(members: TCHMembers) -> Promise<TCHMemberPaginator> {
return Promise { seal in
members.members(completion: { (result, paginator) in
if result == true, let p = paginator {
seal.fulfill(p)
} else {
seal.reject(NSError())
}
})
}
}
I have the following Swift functions:
private func authorizedMutableURLRequest(#ref: String) -> Result<NSMutableURLRequest> {
...
}
public func fetch(#ref: String) -> Result<NSURLRequest> {
switch authorizedMutableURLRequest(ref: ref) {
case .Success(let mutableURLRequestBox):
return .Success(JiveBox(mutableURLRequestBox.value))
case .Failure(let error):
return .Failure(error)
}
}
There must be a better way to write fetch. If this was Java, I'd write:
public Result<? extends NSURLRequest> fetch(String ref) {
return authorizedMutableURLRequest(ref);
}
private Result<NSMutableURLRequest> authorizedMutableURLRequest(String ref) {
...
}
And everything would work correctly. Is there a way to return an unspecified type with an upper bound in Swift like there is in Java?