ios - OCMock 单元测试错误
<p><p>我使用 OCMock 来测试单例方法。我得到“模拟类中不存在这样的方法。” testSingleton 方法的错误和 testSingletonWithBlock 方法的无限循环(屏幕截图,旋转指示器)</p>
<p>编辑:
在此处下载示例项目</p>
<p> <a href="https://drive.google.com/file/d/0B-iP0P7UfFj0LVFpWWpPb3RDZFU/edit?usp=sharing" rel="noreferrer noopener nofollow">https://drive.google.com/file/d/0B-iP0P7UfFj0LVFpWWpPb3RDZFU/edit?usp=sharing</a> </p>
<p> <img src="/image/GTdUZ.png" alt="enter image description here"/> </p>
<p>这是我的实现</p>
<p>经理:</p>
<pre><code>@implementation Manager
+ (Manager *)sharedManager {
static Manager *instance;
dispatch_once_t predicate;
dispatch_once(&predicate, ^{
instance = ;
});
return instance;
}
- (int)getOne {
return 1;
}
- (void)success:(BOOL)success completion:(void(^)(void))completion failure:(void(^)(void))failure {
success ? completion() : failure();
}
</code></pre>
<p> ViewController :</p>
<pre><code>- (void)manager_printOne {
int num = [ getOne];
NSLog(@"number is: %d", num);
}
- (void)manager_success:(BOOL)success completion:(void(^)(void))completion failure:(void(^)(void))failure {
[ success:success completion:completion failure:failure];
}
</code></pre>
<p>测试 ViewController :</p>
<pre><code>@interface coreDataTestTests : XCTestCase
@property (nonatomic, strong) id mockManager;
@property (nonatomic, strong) ViewController *viewController;
@end
@implementation coreDataTestTests
- (void)setUp
{
;
self.viewController = ;
//for singleton
self.mockManager = ;
}
- (void)tearDown
{
;
self.viewController = nil;
//Note: singleton need both, retain counts = 2
self.mockManager = nil;
;
}
- (void)testSingleton {
NSLog(@"testSingleton");
OCMStub().andReturn(2);
;
}
- (void)testSingletonWithBlock {
NSLog(@"testSingletonWithBlock");
[[[ ignoringNonObjectArgs] andDo:^(NSInvocation *invocation) {
void(^block)(void);
;
block();
}] success:0 completion: failure:];
[self.viewController manager_success:NO completion:^{
NSLog(@"completion");
} failure:^{
NSLog(@"failure");
}];
}
@end
</code></pre>
<p>单元测试的经理类别:</p>
<pre><code>static Manager *mockManager = nil;
@implementation Manager
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (Manager *)sharedManager {
if (mockManager) {
return mockManager;
}
return invokeSupersequentNoParameters();
}
#pragma clang diagnostic pop
+(id)createMockManager {
mockManager = ];
return mockManager;
}
+(id)createNiceMockManager {
mockManager = ];
return mockManager;
}
+(void)releaseInstance {
mockManager = nil;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以只 stub<code>sharedManager</code> 并返回一个不错的模拟,而不是创建一个类别。</p>
<pre><code>- (void)setUp
{
;
self.viewController = ;
//for singleton
id classMockManager = OCClassMock();
OCMStub().andReturn(classMockManager);
self.mockManager = classMockManager;
}
</code></pre>
<p>我目前还没有一个环境来测试这个,但这个策略应该有效。请注意,这是 OCMock3 语法。见 <a href="http://ocmock.org/reference/#mocking-class-methods" rel="noreferrer noopener nofollow">http://ocmock.org/reference/#mocking-class-methods</a> </p></p>
<p style="font-size: 20px;">关于ios - OCMock 单元测试错误,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25588215/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25588215/
</a>
</p>
页:
[1]