How do you test a NWProtocolFramerImplementation class?

Since the framework manages the lifetime of an instance of a type conforming to NWProtocolFramerImplementation, plus the streaming of input and output into/from the instance, how can testing be done? My example type needs a StreamProtocol object for the underlying bytes; can mocks be created?

Answered by DTS Engineer in 913002022

The short answer here is that this isn’t easy. Whenever you test code like this your are faced with two issues:

  • Does your code do what you think it does?
  • Does the network do what you think it does?

Writing tests for the first is easy. You pull your code code out into a separate type and write a unit test for it.

Any tests for the second are integration tests, by definition, because your testing how your code integrates with the networking stack. That means that heading off into MockLand™ is a mistake, IMO, because your mocks will be based on your assumptions about how the networking stack works, and those are the same set of assumptions your used to write your code and thus they are already being tested by your unit test. So, you need an actual integration test, and that means adding your framer to a connection, connecting to a test server, and exercising various corner cases.

Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The short answer here is that this isn’t easy. Whenever you test code like this your are faced with two issues:

  • Does your code do what you think it does?
  • Does the network do what you think it does?

Writing tests for the first is easy. You pull your code code out into a separate type and write a unit test for it.

Any tests for the second are integration tests, by definition, because your testing how your code integrates with the networking stack. That means that heading off into MockLand™ is a mistake, IMO, because your mocks will be based on your assumptions about how the networking stack works, and those are the same set of assumptions your used to write your code and thus they are already being tested by your unit test. So, you need an actual integration test, and that means adding your framer to a connection, connecting to a test server, and exercising various corner cases.

Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"