質問

I'm trying to test a class that implements two class methods from NSURLConnection, but keep getting the error:

error: testSyncConnection (MyURLConnectionTest) failed: *** -[NSProxy doesNotRecognizeSelector:sendSynchronousRequest:returningResponse:error:] called!

Here's the interface for MyURLConnection:

@interface MyURLConnection : NSURLConnection

+ (void) sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue*) queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler;
+ (NSData *) sendSynchronousRequest:(NSURLRequest *)request returningResponse (NSURLResponse **)response error:(NSError **)error;

@end

Here's how i'm trying to Mock it:

- (void)testSyncConnection
{

    id testConnection = [OCMockObject mockForClass:[MyURLConnection class]];

    [[[testConnection stub] andReturn:Nil] sendSynchronousRequest:Nil
                                                returningResponse:Nil
                                                            error:Nil];

    //rest of test...

}

which fails at the stubbing part, producing the error I posted above..

I also made sure the MyDURLConnection implementation is added to the Test target too.

Any tips what I'm missing here ?

役に立ちましたか?

解決

Which version of OCMock are you using? Mocking class methods is supported since 2.1.

Apart from that, this works for me:

id testConnection = [OCMockObject mockForClass:[MyURLConnection class]];

[[[testConnection stub] andReturn:nil] sendSynchronousRequest:nil
                                            returningResponse:nil
                                                        error:NULL];

STAssertNil([MyURLConnection sendSynchronousRequest:nil returningResponse:nil error:NULL], nil);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top