Update,
Moya method is good
Moya’s testing features are particularly well designed. Through sampleData
Use it like this:
Set up the
// The normal part is staticletNetHud = MoyaProvider< netHud >()let stubbingProvider = MoyaProvider<NetHud>(stubClosure: MoyaProvider.immediatelyStub)
var sampleData: Data {
var example = "Half measures are as bad as nothing at all.".data(using: String.Encoding.utf8)!
switch self {
case .test:
do {
if let path = Bundle.main.url(forResource: "one", withExtension: "plist"){
example = try Data(contentsOf: path)
}
} catch {}
default:
break
}
return example
}
Copy the code
call
func request(_ callback: @escaping (General<Data>) -> Void){
let debug = true
if debug{
stubbingProvider.request(.test){ (result) in
do{
let data = try result.get().data
let dict = try PropertyListSerialization.propertyList(from:data, format: nil) as! [String:Any]
// handle data
let decoder = PropertyListDecoder()
let info = try decoder.decode(GeneralSingle<PracticeList>.self, from: data)
callback(info)
}
catch let error as NSError{
print(error)
}
}
}
else{
netHud.request(.test){ (result) in
do{
let data = try result.get().data
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
// handle data
let info = try Base.decoder.decode(GeneralSingle<PracticeList>.self, from: data)
callback(info)
}
catch let error as NSError{
print(error)
}
}
}
}
Copy the code
The original:
Background: Back end engineers are sometimes too busy to prepare the right development data for front end engineers,
At this point, the Mock Server is needed
In two steps,
-
Obtain data and modify data according to your own needs
-
Provide data
Step one, get the data
Modify according to your own development requirements
First remove the JSON requested by the interface from the library,
// Convert json to plist and put it in the Document directory of the sandboxdo{
let json = try JSONSerialization.jsonObject(with: self, options: JSONSerialization.ReadingOptions())
if let dict = json as? [String: Any]{
let src = dict as NSDictionary
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let path = documentsURL.appendingPathComponent("one.plist")
let result = src.write(to: path, atomically: true)
print(result)
}
}
catch let error{
print(Gg, \ "(error)")}Copy the code
If you go straight to the emulator, print path, you’ll find the plist file on your Mac.
You can go straight to the real machine and synchronize files to the Mac via Woodpecker.
Second, provide data
First, add the downloaded JSON file to the project.
When you run it, it’s in the Bundle
func request(_ callback: @escaping (GeneralSingle<PracticeList>) -> Void){
let debug = true
guard debug == false else{// New interface request logic, local, included in the Bundle plist filedo{
if let path = Bundle.main.url(forResource: "one", withExtension: "plist") {let data = try Data(contentsOf: path)
let decoder = PropertyListDecoder()
let info = try decoder.decode(GeneralSingle<PracticeList>.self, from: data)
callback(info)
}
}
catch let error as NSError{
print(error)
}
return} // Original logic, go network request AF. Request (requseturl. youOwn, method:.get, headers: AF. Bearer).response {(resp)in
// ...
Copy the code