dri*_*iis 8 mocking objective-c ocmock ios
考虑这个有效的代码(loginWithEmail方法得到预期的,好的,预期的):
_authenticationService = [[OCMockObject mockForClass:[AuthenticationService class]] retain];
[[_authenticationService expect] loginWithEmail:[OCMArg any] andPassword:[OCMArg any]];
Run Code Online (Sandbox Code Playgroud)
与此代码对比:
_authenticationService = [[OCMockObject mockForProtocol:@protocol(AuthenticationServiceProtocol)] retain];
[[_authenticationService expect] loginWithEmail:[OCMArg any] andPassword:[OCMArg any]];
Run Code Online (Sandbox Code Playgroud)
第二个代码示例在第2行失败,出现以下错误:
*** -[NSProxy doesNotRecognizeSelector:loginWithEmail:andPassword:] called! Unknown.m:0: error: -[MigratorTest methodRedacted] : ***
-[NSProxy doesNotRecognizeSelector:loginWithEmail:andPassword:] called!
Run Code Online (Sandbox Code Playgroud)
AuthenticationServiceProtocol声明方法:
@protocol AuthenticationServiceProtocol <NSObject>
@property (nonatomic, retain) id<AuthenticationDelegate> authenticationDelegate;
- (void)loginWithEmail:(NSString *)email andPassword:(NSString *)password;
- (void)logout;
- (void)refreshToken;
@end
Run Code Online (Sandbox Code Playgroud)
它在课堂上实现:
@interface AuthenticationService : NSObject <AuthenticationServiceProtocol>
Run Code Online (Sandbox Code Playgroud)
这是使用OCMock for iOS.
expect
当模拟是一个时,为什么会失败mockForProtocol
?
这很奇怪。我已将以下类添加到 iOS5 示例项目中:
@protocol AuthenticationServiceProtocol
- (void)loginWithEmail:(NSString *)email andPassword:(NSString *)password;
@end
@interface Foo : NSObject
{
id<AuthenticationServiceProtocol> authService;
}
- (id)initWithAuthenticationService:(id<AuthenticationServiceProtocol>)anAuthService;
- (void)doStuff;
@end
@implementation Foo
- (id)initWithAuthenticationService:(id<AuthenticationServiceProtocol>)anAuthService
{
self = [super init];
authService = anAuthService;
return self;
}
- (void)doStuff
{
[authService loginWithEmail:@"x" andPassword:@"y"];
}
@end
@implementation ProtocolTests
- (void)testTheProtocol
{
id authService = [OCMockObject mockForProtocol:@protocol(AuthenticationServiceProtocol)];
id foo = [[Foo alloc] initWithAuthenticationService:authService];
[[authService expect] loginWithEmail:[OCMArg any] andPassword:[OCMArg any]];
[foo doStuff];
[authService verify];
}
@end
Run Code Online (Sandbox Code Playgroud)
当我在 Xcode 版本 4.5 (4G182) 中针对 iPhone 6.0 模拟器运行此测试时,测试通过了。模拟对象的使用方式有什么不同吗?在您的情况下, _authenticationService 传递到哪里?收件人正在对它做什么?
归档时间: |
|
查看次数: |
2526 次 |
最近记录: |