OCMock unit test error

OMGPOP

I use OCMock to test out singleton methods. I get "no such method exists in the mocked class." error for testSingleton method and infinite loop (the screenshot, the spinning indicator) for testSingletonWithBlock method

EDIT: download sample project here

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

enter image description here

Here is my implementation

manager:

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

view controller:

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

- (void)manager_success:(BOOL)success completion:(void(^)(void))completion failure:(void(^)(void))failure {

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

}

test 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

manager category for unit test:

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;
}
Ben Flynn

Rather than creating a category, you could just stub sharedManager and return a nice mock.

- (void)setUp
{
    [super setUp];

    self.viewController = [ViewController new];

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

I don't have an environment up to test this just this moment, but this strategy should work. Note that this is OCMock3 syntax. See http://ocmock.org/reference/#mocking-class-methods

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

OCMock test category methods

From Dev

OCMock link error

From Dev

AngularJS Unit Test Error dependence

From Dev

VSTS Unit test error message

From Dev

OCMock test not getting compiled with XCTest

From Dev

Error in test method in mvc for routing unit test

From Dev

Mocha Unit Test Error Handling from Library

From Dev

Suppress supplementary error message from unit test

From Dev

Angular Unit Test: Error: Unexpected request: GET

From Dev

Karma unit test 'undefined' is not a function error

From Java

MatDialog Service Unit Test Angular 6 Error

From Java

Error calling Dispatchers.setMain() in unit test

From Dev

karma unit test runner directory injector error

From Dev

Force a unit test case to ERROR in Python

From Dev

Catch unit test library linking error

From Dev

Controller Unit Test fails with SQLSTATE[42000] error

From Dev

unable to get error from SeeJson unit test

From Dev

Module not found error in AngularJS unit test

From Dev

Angular Unit Test Jasmine Spy error

From Dev

Boost unit test link error -- abi mismatch?

From Dev

Force a yielded function to error for unit test

From Dev

Linking error in unit test using URLRequestConvertible

From Dev

Angular Unit Test Jasmine Spy error

From Dev

Error in Unit Test MVC Controller using Fake

From Dev

Karma unit test 'undefined' is not a function error

From Dev

ASP Core Identity - Unit test In Memory Error

From Dev

Understand mvn unit test error with JSON MockHttpServletResponse Test

From Dev

OCMock expect and return gives signature error

From Dev

LINQ to Entities error occurring at run time but not in unit test