ocmock

OCMock - verifying order of method calls. Is there any other way?

三世轮回 提交于 2019-12-22 05:28:14
问题 When I want to verify that in one method a mock object is receiving some messages in a particular order I do something like this: // sut is an instance of the class I am testing and myMock is a mock object injected in sut. // I want to test that myMock sends messageA and then messageB, in that particular order. [[[myMock expect] andDo:^(NSInvocation *invocation) { [[myMock expect] messageB]; }] messageA]; [sut methodToTest]; [myMock verify]; Is there any cleaner/better way of doing this?

stub [[SomeClazz alloc] init] not work but accepted answer says it should work

天大地大妈咪最大 提交于 2019-12-21 20:17:07
问题 My function under test is very simple: @implementation MyHandler ... -(void) processData { DataService *service = [[DataService alloc] init]; NSDictionary *data = [service getData]; [self handleData:data]; } @end I use OCMock 3 to unit test it. I need to stub the [[DataService alloc] init] to return a mocked instance , I tried the answer from this question (which is an accepted answer) to stub [[SomeClazz alloc] init] : // Stub 'alloc init' to return mocked DataService instance, // exactly

Using a struct with OCMock or Hamcrest

混江龙づ霸主 提交于 2019-12-21 09:19:22
问题 I'm hitting a road block and I'm wondering if the brilliant collective minds here can help. In ObjC CocoaTouch I'm trying to mock an object that takes struct parameters and returns a struct. OCMock is coughing up a hair-ball so I tried wrapping with a Hamcrest matcher. No die. The function/method I'm testing looks something like this: - (CLLocationCoordinate2D)pixelToLatLong:(CGPoint)aPoint; I use code like this: #define OCMOCK_STRUCT(atype, variable) [NSValue value:&variable withObjCType:

In unit test, execute the block passed in queue with dispatch_asyc

徘徊边缘 提交于 2019-12-21 03:01:29
问题 If I dispatch_async a block on main queue like this: -(void) myTask { dispatch_async(dispatch_get_main_queue(), ^{ [self.service fetchData]; }); } In unit test, I can execute the block passed in main queue by manually run the main loop like this: -(void)testMyTask{ // call function under test [myObj myTask]; // run the main loop manually! [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; // now I can verify the function 'fetchData' in block is called ... } Now

In unit test, execute the block passed in queue with dispatch_asyc

戏子无情 提交于 2019-12-21 03:01:21
问题 If I dispatch_async a block on main queue like this: -(void) myTask { dispatch_async(dispatch_get_main_queue(), ^{ [self.service fetchData]; }); } In unit test, I can execute the block passed in main queue by manually run the main loop like this: -(void)testMyTask{ // call function under test [myObj myTask]; // run the main loop manually! [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; // now I can verify the function 'fetchData' in block is called ... } Now

Test rig exited abnormally with code 134 with OCMock verify on iOS 4

左心房为你撑大大i 提交于 2019-12-18 17:55:33
问题 I'm trying to add OCMock to my iOS 4 project. To test it out, I have a class Person with one method, -hello . When I run this test: - (void) testMock { id mock = [OCMockObject mockForClass:[Person class]]; [[mock expect] hello]; [mock hello]; [mock verify]; } Everything is fine, and the build succeeds. If I take away the hello call, like this: - (void) testMock { id mock = [OCMockObject mockForClass:[Person class]]; [[mock expect] hello]; [mock verify]; } I'd expect to get an error message

Unit testing Parse framework iOS

强颜欢笑 提交于 2019-12-18 05:13:07
问题 I am attempting to write unit tests for an iOS application which makes use of the Parse backend framework, and after much experimentation seem to be failing at writing successful unit tests. I have found a few posts on testing asynchronous code (Testing asynchronous call in unit test in iOS) and testing network calls, but am yet to find a way of testing calls to the Parse backend with async callbacks. To give an example, could anyone advise how I would test the following line of code: [PFUser

how to unit testing AFNetworking request

假如想象 提交于 2019-12-18 04:09:32
问题 i am making a GET request to retrieve JSON data with AFNetworking as this code below : NSURL *url = [NSURL URLWithString:K_THINKERBELL_SERVER_URL]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; Account *ac = [[Account alloc]init]; NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:[NSString stringWithFormat:@"/user/%@/event/%@",ac.uid,eventID] parameters:nil]; AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request

OCMock testing the address of a struct

有些话、适合烂在心里 提交于 2019-12-12 21:11:39
问题 I have some code I want to test that is passing around the address of a struct: MyObject myObject = ...; MyRecord record = [someObject record]; //record is a @property [myObject add:&record]; I've mocked someObject to return a record: MyRecord record = ...; [[[_mockSomeObject stub] andReturnValue:OCMOCK_VALUE(record)] record]; [[_mockMyObject expect] add:&record]; Unfortunately, OCMock fails (I believe) because pulling the struct out of the NSValue wrapper will always return a different

OCMock a Class method to call back the passed block with custom Data

别来无恙 提交于 2019-12-12 18:37:48
问题 I do have a class that handles all my network communication like this: typedef void (^ networkEndblock) (NSArray *, NSError *); @interface NetworkAPI : NetworkBase + (void) doSomeNetworkAcion:(networkEndblock) endBlock; @end I use the above code like this (I do not want to get into irrelevant details here) - (void) runMyProcess:(SomeEndBlock)proccesEnded{ // Bussiness logic // Get Data from the web [NetworkAPI doSomeNetworkAcion:^(NSArray *resultArray, NSError *error){ // Check for error. if