Objective-C:用于测试目的的 NSInputStream 和 NSOutputStream
我的应用程序中有一个使用 NSInputStream 初始化的类。出于测试目的,我想在 NSOutputStream 上写入数据,然后由该输入流接收数据。 这应该触发 NSStreamEventHasBytesAvailable 事件。
唯一的问题是我不知道如何设置。有人有想法吗?或者建议如何对具有 NSStream 作为依赖项的类进行单元测试。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上并不是很棘手,我已经使用苹果开发人员网站上提供的 SimpleURLConnection 示例代码描述了如何做到这一点。
这就是问题,我的答案也适用于此。
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.
这实际上非常棘手,因为看起来您只能使用文件、URL 或 NSData 创建输入流。尽管您可以编写一个实现这两个接口的类,但您无法将输入流连接到输出流。
进行单元测试的最简单方法可能是从已从输出流读取其字节的
NSData
创建输入流。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.