Я заранее ценю любое мнение по этому вопросу. Спасибо! < /P>
Код: Выделить всё
SFBConfigModelКод: Выделить всё
internal struct SFBConfigModel: Codable {
var config: SFBIntervalModel
var binding: SFBBindingModel?
private enum CodingKeys: String, CodingKey {
case config
case binding
}
init(
config: SFBIntervalModel,
binding: SFBBindingModel?
) {
self.config = config
self.binding = binding
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// Searches for the config value which LocalStorage can support.
self.config = try container.decode(SFBIntervalModel.self, forKey: .config)
if let binding = try? container.decode(SFBBindingModel.self, forKey: .binding) {
self.binding = binding
}
}
}
< /code>
el test: < /p>
context("when creating the model with a invalid json") {
it("then you get a decode error") {
// Given
let JSONData = """
{
"config": {
"interval_time_in_minutes": "thisIsAnUnexpectedTypeOfValue"
},
"binding": {
"seed": "thisIsOtherFakeSeed",
"device_id": "thisIsOtherFakeDeviceID"
}
}
""".data(using: .utf8)!
let decoder = JSONDecoderMock()
var model: SFBConfigModel!
do {
// When
model = try decoder.decode(
SFBConfigModel.self,
from: JSONData
)
} catch {
// Then
expect("\(error)").to(contain("JSONDecoderErrorDomainMock"))
}
}
}
< /code>
JSONDecoderMockprivate class JSONDecoderMock: JSONDecoder {
var decodeCalled = false
override func decode(_ type: T.Type, from data: Data) throws -> T where T : Decodable {
decodeCalled = true
throw NSError(domain: "JSONDecoderErrorDomainMock", code: -1, userInfo: nil)
}
}
< /code>
Image of the error on the server that does not occur locally:

I have changed the way of decoding, I added a mock but the error persists even though it does not play locally.
Подробнее здесь: https://stackoverflow.com/questions/794 ... ts-for-ios
Мобильная версия