2

I'm writing unit test for something that has a required initializer that takes a Decoder as parameter, the decoder isn't used in my test and the object is also only a dummy placeholder for something else, however I need to provide some instance that conforms to Decoder protocol, I checked JSONDecoder but it doesn't conform to the protocol, then I thought about creating a lightweight class that conforms to Decoder and basically has empty protocol methods / properties. Now, having to implement functions like:

func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey

func singleValueContainer() throws -> SingleValueDecodingContainer

func unkeyedContainer() throws -> UnkeyedDecodingContainer

feels to me like an overkill since I still have to construct objects to return, is there any existing/native class that conforms to Decoder so I can use as a dummy object for unit test?

Thanks!

Edit: The reason for using this, is that I have an object (the one with initializer takes a Decoder, say ObjA), the object itself is a non-nil property of another object (say ObjB) and the unit test is to do with the other object, as a result I need a dummy ObjA which need a decoder to initialize.

Heuristic
  • 5,087
  • 9
  • 54
  • 94
  • Can you explain why this thing has a required initializer that takes a Decoder as parameter in the first place? – matt Mar 11 '18 at 01:39
  • When you declare a struct Decodable and implement `init(from:)`, the parameter is a Decoder. So there's an obvious way to obtain one. – matt Mar 11 '18 at 01:55
  • If the `Decoder` isn’t used, why not `fatalError` in the implementations of those methods? They shouldn’t be called, and you won’t need to return anything. – Itai Ferber Mar 11 '18 at 03:24
  • @matt thanks I've updated the question. – Heuristic Mar 11 '18 at 11:36

0 Answers0