Recommended way to test mappings
Closed this issue · 1 comments
ogezue commented
In my project I have written some model classes that fetch data via Alamofire, Objectmapper and Realm. Sometimes a backend developer changes the REST-interface or a dog walks across my keyboard, so that my mapping breaks.
I would like to increase my test coverage and detect if my mapping doesn't fit to the REST-service anymore. What would be the best approach to test this?
Here is a method thats would be great to have tested:
func fetchOverviewItemList(successHandler: List<OverviewItem> -> (), errorHandler: (ErrorType?) -> ()){
//fetch existing objects from DB
let overviewItemsAsList = List<OverviewItem>()
overviewItemsAsList.appendContentsOf(self.realm.objects(OverviewItem))
successHandler(overviewItemsAsList)
AlamofireManager.Configured
.request(.GET, URLs.sharedInstance.overviewItemsUrl())
.responseJSON{response in log.verbose("response: \(response.result.value)")}
.responseArray("sefInfos") { (response: Response<[OverviewItem], NSError>) in
if let error = response.result.error{
log.error("Error: \(error.localizedDescription)")
errorHandler(error)
return
}
if let overviewItemsArray = response.result.value{
//remove orphans
self.deleteOrphans(overviewItemsAsList, fetchedData: overviewItemsArray)
//update objects from service
overviewItemsAsList.removeAll()
overviewItemsAsList.appendContentsOf(overviewItemsArray)
successHandler(overviewItemsAsList)
do{
try self.realm.write{
self.realm.add(overviewItemsAsList, update: true)
}
}
catch let err as NSError {
log.error("Error with realm: \(err.localizedDescription)")
}
}
}
}
tristanhimmelman commented
Closing this as I answered your same question in the ObjectMapper project