iPhone 命令行单元测试(和文件 I/O)
简单的问题是:如何通过命令行单元测试使 iPhone(objective-c)文件操作正常工作?
长问题,带解释:这最终将成为一个脚本,用于通过 Hudson 实例为我的 iPhone 构建执行自动构建/测试。按照makdad的链接这个所以问题允许我从命令行(半)成功运行单元测试。
然而,我的一项测试失败了。该测试将调用缓存服务类来保存文件,然后尝试检索它。但是,从命令行运行测试时,文件 I/O 似乎不起作用:(。
作为参考,通过 Xcode GUI 运行单元测试不会导致此类错误。
我正在使用 NSFileHandle 方法调用来获取用于写入的句柄。如果它们返回 nil,则该文件是使用创建的,
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
我认为这可能与模拟器的缓存目录路径中的空格有关,如果是这样,我将如何纠正这个问题
?需要是NOT 为了使其正常工作,模拟器以编程方式启动,并且如果正在运行,则不会显示 GUI,命令行构建失败。
The short question is: How can I get iPhone (objective-c) file operations to work correctly from a command line Unit Test?
The long question, with explanation: This will eventually become a script to perform automated building/testing for my iPhone build, via a Hudson instance. Following makdad's link on this SO question has allowed me to run Unit tests from the command line (semi) successfully.
However, one of my tests fails. The test would call a Caching Service class to save a file, then try and retrieve it. however, file I/O appears to not work when running the tests from the command line :(.
For Reference, running the Unit tests via the Xcode GUI results in no such errors.
I am using NSFileHandle method calls to get handles for writing. if they return nil, the file is created using
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
I thought it may have to do with the spaces in the path to the simulator's cache directory. am I on the right track? if so, how would i rectify this?
Note also that the simulator needs to be NOT running already in order for this to work, the simulator is started programmatically and does not display a GUI. if it is running, the command line build fails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先:“模拟器需要尚未运行才能正常运行”
我在终端中运行测试,模拟器是否打开并不重要。
也许您需要查看的一些构建设置是:
TEST_HOST
和BUNDLE_LOADER
。我在 xcodeproj 中将它们留空。
注意:我还使用 Hudson 以及测试报告和代码覆盖率。
第二:
我在使用加载路径测试终端和应用程序时遇到过失败。这与从资源加载的核心数据模型有关。
解决方案是从 url 而不是路径加载文件:
我无法确保这与您在使用 NSFileManager 时遇到的相同问题有关,但我只能想象 NSBundle 使用 NSFileManager。 (所以这可以相关)
第三:
不要让您的测试依赖于 I/O。
我发现这不是单元测试的目的。此类测试可能不依赖于文件系统、数据库、网络连接等。
创建一个在运行测试时模拟的文件系统抽象类。
这样,您的实现仅在一处依赖于实际文件系统,您可以在测试期间替换该文件系统。
您只需要一个测试来检查该抽象。
总结
希望这有帮助。
First: 'the simulator needs to be NOT running already in order for this to work'
I have my tests running in the terminal and it doesn't matter if the simulator is on.
Maybe some build settings you have to look at are:
TEST_HOST
andBUNDLE_LOADER
.I leave them empty in my xcodeproj.
Note: I'm using Hudson as well with test reports and code coverage.
Second:
I have experienced failures in tests terminal and application with loading paths. This was related to the Core Data model which is loaded from a resource.
The solution was to load the file from url instead of a path:
I cannot ensure this relates to the same problem your encountering with the NSFileManager, but i can only imagine that NSBundle makes use of the NSFileManager. (So this can be related)
Third:
Do not make your tests dependent on I/O.
I find that that is not the purpose of a Unit Test. Such test may not rely on a filesystem, database, network connection, etc.
Make an file system abstraction class that you mock while running your tests.
This way your implementation is only at one place relying on the actual file system, which you can replace during testing.
You only need one test to check that abstraction.
Summary
Hope this was helpful.