ocmock

How to test if a method of an object is called inside completion handler block using OCMock?

a 夏天 提交于 2020-01-23 10:48:05
问题 I have a method: @implementation SomeClass - (void)thisMethod:(ObjectA *)objA { [APIClient connectToAPIWithCompletionHandler:^(id result){ if (result) [objA methodOne]; else [objA methodTwo]; }]; } Is there a way to verify methodOne or methodTwo will be called when thisMethod: is called? Basically I just want to stub that connectToAPIWithCompletionHandler: method. Right now I can do this by swizzling connectToAPIWithCompletionHandler: method. But I want to know if there's better way. I found

How to mock an object with OCMock which isn't passed as a parameter to method?

 ̄綄美尐妖づ 提交于 2020-01-19 08:01:59
问题 I have a method I would like to test with OCMock but not sure how to do that. I need to mock ExtClass which isn't defined as part of my code (external library): +(NSString *)foo:(NSString *)param { ExtClass *ext = [[ExtClass alloc] initWithParam:param]; if ([ext someMethod]) return @"A"; else return @"B"; } Thanks in advance! 回答1: OCMock 2 id mock = [OCMockObject mockForClass:[ExtClass class]]; // We stub someMethod BOOL returnedValue = YES; [[[mock stub] andReturnValue:OCMOCK_VALUE

How to mock an object with OCMock which isn't passed as a parameter to method?

雨燕双飞 提交于 2020-01-19 08:01:09
问题 I have a method I would like to test with OCMock but not sure how to do that. I need to mock ExtClass which isn't defined as part of my code (external library): +(NSString *)foo:(NSString *)param { ExtClass *ext = [[ExtClass alloc] initWithParam:param]; if ([ext someMethod]) return @"A"; else return @"B"; } Thanks in advance! 回答1: OCMock 2 id mock = [OCMockObject mockForClass:[ExtClass class]]; // We stub someMethod BOOL returnedValue = YES; [[[mock stub] andReturnValue:OCMOCK_VALUE

OCMock test fails - expected method was not invoked

梦想与她 提交于 2020-01-14 06:37:11
问题 IN test class -(void)testMyTest { MasterViewController* masterVC = [[MasterViewController alloc]init];//[OCMockObject mockForClass:[MasterViewController class]]; id master = [OCMockObject mockForClass:[DetailViewController class]]; [[master expect] getStringVal:@"PARAM"]; [masterVC doSomething]; [master verify]; } IN detailViewController -(NSString*)getStringVal:(NSString*)param { NSString *returnParam = [NSString stringWithFormat:@"%@-String",param]; return returnParam; } IN Master view

How to partially mock external object

大兔子大兔子 提交于 2020-01-06 19:25:49
问题 I have class method to test with dependant object (Keys object) APIRouter.m + (NSURL*)apiURLWithPath:(NSString*)path { MyKeys *keys = [MyKeys new]; NSString *url = [NSString stringWithFormat:@"%@?api_key=%@", path, [keys APIKey]]; return [NSURL URLWithString:url]; } I am trying to partially mock this Keys object and return "MY_API_KEY" value but the test method fails and returns real API key (e.g. as78d687as6d7das8da). APIRouterSpec.m describe(@"APIRouter", ^{ it(@"should return url for api",

How to partially mock an object inside legacy code with OCMock?

心已入冬 提交于 2020-01-06 14:51:11
问题 I would like to accomplish what also is described here, i.e create mocks inside legacy code. However I require partial instead of nice or strict mocks. For example, consider leaderboards that behave exactly like GKLeaderbaord except for implementing a stubbed version of loadScoresWithCompletionHandler: . I've tried this code inside an XCTestCase but it currently fails at runtime in the indicated line: OCMInvocationMatcher raises an EXC_BAD_ACCESS error. Perhaps there is some infinite

Mock NSHTTPURLRequest and NSHTTPURLResponse in iOS unit test

六月ゝ 毕业季﹏ 提交于 2020-01-03 17:07:14
问题 I'm developing a framework in iOS which makes HTTP calls to server.I wanted to write unit test to test the API's.I wanted to mock the server calls,without actually making real server call.Can anyone help me with sample unit test which makes mocked server calls.How to do we set expectations and return the hand constructed url response.?I'm using XCTest framework for unit test and OCMock for mocking objects. 回答1: Here is how you might mock sendAsynchronousRequest: NSDictionary *serverResponse =

Adding OCMock causes Test to launch main app instead of running tests

谁说我不能喝 提交于 2020-01-03 10:56:10
问题 I'm trying to add OCMock to my existing Cocoa project, but am running to a strange problem that I don't see anyone else cover. I finally isolated it to the following: if I simply add OCMock.framework reference to my project (i.e. drag it in somehow into Link Binary With Libraries build phase), when I run tests, the real app gets launched instead. Without OCMock, the output is normal: Test Suite 'Multiple Selected Tests' started at 2013-02-07 20:07:03 +0000 With OCMock framework link (partial

OCMock an NSOperation

↘锁芯ラ 提交于 2020-01-02 04:46:23
问题 I am trying to write some Unit Tests to test some custom NSOperations that we are writing. What I'd like to do is create a Mock of the NSOperation and put it on the NSOperationQueue , and then wait for it to finish. I know I can swizzle the methods and not use OCMoc k at all, but I really don't want to do that. I'd like to use OCMock . The code I'm trying to run is something like the following: MYOperation *operation = [MYOperation new]; id mockOperation = [OCMockObject partialMockForObject

How to use Core Data for Dependency Injection

无人久伴 提交于 2020-01-01 12:10:30
问题 I'm toying with using Core Data to manage a graph of objects, mainly for dependency injection (a subset of the NSManagedObjects do need to be persisted, but that isn't the focus of my question). When running unit tests, I want to take over creation of the NSManagedObjects, replacing them with mocks. I do have a candidate means of doing this for now, which is to use the runtime's method_exchangeImplementations to exchange [NSEntityDescription insertNewObjectForEntityForName