【发布时间】:2012-09-27 09:34:15
【问题描述】:
考虑一下这段代码,它可以工作(loginWithEmail 方法正如预期的那样):
_authenticationService = [[OCMockObject mockForClass:[AuthenticationService class]] retain];
[[_authenticationService expect] loginWithEmail:[OCMArg any] andPassword:[OCMArg any]];
对比这段代码:
_authenticationService = [[OCMockObject mockForProtocol:@protocol(AuthenticationServiceProtocol)] retain];
[[_authenticationService expect] loginWithEmail:[OCMArg any] andPassword:[OCMArg any]];
第二个代码示例在第 2 行失败,出现以下错误:
*** -[NSProxy doesNotRecognizeSelector:loginWithEmail:andPassword:] called! Unknown.m:0: error: -[MigratorTest methodRedacted] : ***
-[NSProxy doesNotRecognizeSelector:loginWithEmail:andPassword:] called!
AuthenticationServiceProtocol 声明方法:
@protocol AuthenticationServiceProtocol <NSObject>
@property (nonatomic, retain) id<AuthenticationDelegate> authenticationDelegate;
- (void)loginWithEmail:(NSString *)email andPassword:(NSString *)password;
- (void)logout;
- (void)refreshToken;
@end
并且在类中实现:
@interface AuthenticationService : NSObject <AuthenticationServiceProtocol>
这是在 iOS 上使用 OCMock。
当模拟是mockForProtocol 时,为什么expect会失败?
【问题讨论】:
-
您是从源代码构建 OCMock 吗?在
OCProtocolMockObject的methodSignatureForSelector:方法中放置一个断点会很有趣。 -
不是从源代码构建,只是下载了静态库。
-
你能发布实际的协议声明吗?
-
是的,刚刚编辑了问题以包含正在使用的完整的实际协议。
标签: objective-c ios mocking ocmock