2

I have an class in my application that is initialized with an NSInputStream. For testing purposes i want to write data on an NSOutputStream which then is received by that input stream. This should trigger the NSStreamEventHasBytesAvailable event.

The only thing is that i do not know how to set this up. Does anybody has an idea? Or suggestions how to unit test a class with has an NSStream as dependency.

Thanks

Mats Stijlaart
  • 5,058
  • 7
  • 42
  • 58

2 Answers2

2

This is actually not very tricky, I've described how to do it using the SimpleURLConnection sample code available on the apple developer site.

This is the question and my answer there will work here too.

Community
  • 1
  • 1
JugsteR
  • 1,144
  • 8
  • 22
1

This is actually really tricky since it appears that you can only create an input stream using a file, a URL or an NSData. You can't connect an input stream to an output stream, although you might be able to write a single class that implements both interfaces.

The easiest way to do the unit test would probably be to create your input stream from an NSData whose bytes you have already read in from the output stream.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Thanks, i will go for the single class solution. Is the most logical i think. – Mats Stijlaart Aug 06 '11 at 11:37
  • This answer is misleading and wrong. You can connect streams as outlined in the link in my answer. – JugsteR Sep 11 '12 at 14:19
  • @JugsteR I think you'll find my answer will work so it's not wrong, although I accept that the bit about linking an input stream to an output stream is misleading if you allow yourself to use technologies other than Cocoa – JeremyP Sep 13 '12 at 15:41
  • It will perhaps work, that is because you suggest something different than what he is asking for. The wrong part is you say two streams cannot be connected, when they clearly can. The misleading part(s) are that you say it is tricky and that your solution is the easiest way. I guess what is easy is in the eye of the beholder, but my answer shows it is indeed possible to connect two streams and that it is neither tricky nor hard (with example code in the linked answer) – JugsteR Sep 18 '12 at 09:45