iPhone单元测试OCMock,如何模拟只读变量?
我们总是这样使用OCMock,但是对于一些只读属性变量似乎不起作用,比如NavigationController等,它是只读的,设置mock一不生效。
您是否遇到过这个棘手的问题,您是如何解决的?任何想法将不胜感激,提前感谢你们聪明的家伙。
id mockWebView = [OCMockObject mockForClass:[UIWebView class]];
[[mockWebView expect] loadHTMLString:...];
object_setInstanceVariable(viewController, "webView", mockWebView);
We always use OCMock in the following way, but it seems doesn't work for some read only property variables, such as NavigationController and so forth, it is read only, set mock one doesn't take effect.
Have you ever experienced this tough issue, how do you solve it? Any idea will be appreciated, thanks in advance you smart guys.
id mockWebView = [OCMockObject mockForClass:[UIWebView class]];
[[mockWebView expect] loadHTMLString:...];
object_setInstanceVariable(viewController, "webView", mockWebView);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用部分模拟:
如果 viewController 直接访问 ivar,这将不起作用,但如果它通过访问器
self.webview
访问 ivar,则它将起作用,这通常是您应该访问属性的方式。Use a partial mock:
This won't work if viewController accesses the ivar directly, but it will if it accesses the ivar through the accessor:
self.webview
, which is generally how you should access properties.