2

Here is an OCUnit test that simply stubs the method playerID of GKTurnBasedParticipant:

#import <GameKit/GameKit.h>
#import "OnlineMatchTest.h"
#import "OCMock.h"

@implementation OnlineMatchTest

- (void)setUp {
    GKTurnBasedParticipant *participant = [OCMockObject mockForClass:[GKTurnBasedParticipant class]];
    [[[(id)participant stub] andReturn:@"123"] playerID];
}

- (void)test {
    // Do nothing.
}

@end

However, the test fails as if the method -[GKTurnBasedParticipant playerID] did not exist:

Test Case '-[OnlineMatchTest test]' started.
Unknown.m:0: error: -[OnlineMatchTest test] : *** -[NSProxy doesNotRecognizeSelector:playerID] called!
Test Case '-[OnlineMatchTest test]' failed (0.000 seconds).

Why is this happening? I am compiling against the iOS 6.1 SDK, so this method should certainly exist.

Tom
  • 3,831
  • 1
  • 22
  • 24

1 Answers1

2

I haven't been able to determine exactly what this happens, but Apple's documentation of the class saying that you never instantiate it might be a hint that it will not behave as you expect.

One workaround is to create your own object type that fulfills the methods you are interested in:

@interface FakeParticipant : NSObject
@property (nonatomic) id participantID;
@end

@implementation FakeParticipant
@end

...

- (void)testGameKit
{    
    id participant = [OCMockObject mockForClass:[FakeParticipant class]];
    [[[participant expect] andReturn:@"player1" ] participantID];  
}

Since I presume your test has more complexity that this one, you may need to change the code under test to allow you to (partial) mock where you are requesting participant objects.

Ben Flynn
  • 18,524
  • 20
  • 97
  • 142
  • I tried to figure out what's wrong with that class and I was thinking that the reason it is failing is not that you never instantiate it but the fact that there is another property with the same name in GKPlayer class... Just an idea. I hope someone will get us out of doubt. – e1985 Jun 13 '13 at 11:26
  • Thanks, Ben. I'm using a similar workaround. Consistent with @e1985's hypothesis, I experienced the same problem while stubbing `-[GKPlayer playerID]`. – Tom Jun 17 '13 at 16:29