OStack程序员社区-中国程序员成长平台

标题: ios - OCMock 单元测试错误 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:58
标题: ios - OCMock 单元测试错误

我使用 OCMock 来测试单例方法。我得到“模拟类中不存在这样的方法。” testSingleton 方法的错误和 testSingletonWithBlock 方法的无限循环(屏幕截图,旋转指示器)

编辑: 在此处下载示例项目

https://drive.google.com/file/d/0B-iP0P7UfFj0LVFpWWpPb3RDZFU/edit?usp=sharing

enter image description here

这是我的实现

经理:

@implementation Manager
+ (Manager *)sharedManager {
    static Manager *instance;
    dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [Manager new];
    });
    return instance;
}
- (int)getOne {
    return 1;
}
- (void)successBOOL)success completionvoid(^)(void))completion failurevoid(^)(void))failure {
    success ? completion() : failure();
}

View Controller :

- (void)manager_printOne {
    int num = [[Manager sharedManager] getOne];
    NSLog(@"number is: %d", num);
}

- (void)manager_successBOOL)success completionvoid(^)(void))completion failurevoid(^)(void))failure {

    [[Manager sharedManager] success:success completion:completion failure:failure];

}

测试 View Controller :

@interface coreDataTestTests : XCTestCase

@property (nonatomic, strong) id mockManager;

@property (nonatomic, strong) ViewController *viewController;

@end

@implementation coreDataTestTests

- (void)setUp
{
    [super setUp];

    self.viewController = [ViewController new];

    //for singleton
    self.mockManager = [Manager createNiceMockManager];
}

- (void)tearDown
{
    [super tearDown];

    self.viewController = nil;


    //Note: singleton need both, retain counts = 2
    self.mockManager = nil;
    [Manager releaseInstance];

}



- (void)testSingleton {

    NSLog(@"testSingleton");
    OCMStub([self.mockManager getOne]).andReturn(2);
    [self.viewController manager_printOne];

}



- (void)testSingletonWithBlock {

    NSLog(@"testSingletonWithBlock");

    [[[[self.mockHelper stub] ignoringNonObjectArgs] andDo:^(NSInvocation *invocation) {

        void(^block)(void);

        [invocation getArgument:&block atIndex:3];
        block();

    }] success:0 completion:[OCMArg any] failure:[OCMArg any]];


    [self.viewController manager_success:NO completion:^{
        NSLog(@"completion");
    } failure:^{
        NSLog(@"failure");
    }];



}




@end

单元测试的经理类别:

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 = [OCMockObject mockForClass:[Manager class]];
    return mockManager;
}

+(id)createNiceMockManager {
    mockManager = [OCMockObject niceMockForClass:[Manager class]];
    return mockManager;
}

+(void)releaseInstance {
    mockManager = nil;
}



Best Answer-推荐答案


您可以只 stub sharedManager 并返回一个不错的模拟,而不是创建一个类别。

- (void)setUp
{
    [super setUp];

    self.viewController = [ViewController new];

    //for singleton
    id classMockManager = OCClassMock([Manager class]);
    OCMStub([classMockManager sharedManager]).andReturn(classMockManager);
    self.mockManager = classMockManager;
}

我目前还没有一个环境来测试这个,但这个策略应该有效。请注意,这是 OCMock3 语法。见 http://ocmock.org/reference/#mocking-class-methods

关于ios - OCMock 单元测试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25588215/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4