ocmock

Testing class method using OCMock release 2.1.1

☆樱花仙子☆ 提交于 2019-12-04 06:49:46
问题 I am trying to check if a class method is getting invoked using OCMock. I have gathered from OCMock website and other answers on SO that the new OCMock release (2.1) adds support for stubbing class methods. I am trying to do the same: DetailViewController: +(BOOL)getBoolVal { return YES; } Test Case: -(void) testClassMethod { id detailMock = [OCMockObject mockForClass:[DetailViewController class]]; [[[detailMock stub] andReturnValue:OCMOCK_VALUE((BOOL){YES})] getBoolVal:nil]; } The test is

How to mock class method (+)? [duplicate]

旧巷老猫 提交于 2019-12-04 01:38:50
问题 This question already has answers here : Objective C & OC Mock - Mocking a class method? (4 answers) Closed 6 years ago . Need to write unit testing for the following code, I want to do mock for class method canMakePayments, return yes or no, so far no good method found dues to canMakePayments is a class method (+), seems all OCMock methods are all used for instance method (-). You guys any suggestion or discussion will be appreciated. Thanks. // SKPaymentQueue.h // StoreKit if (

Testing controller method with OCMock and Core Data

最后都变了- 提交于 2019-12-03 17:21:33
I am just grasping the concepts of TDD and mocking, and am running into an issue in terms of how to properly. I have a sheet that drops down and lets a user create a new core data object and save it to the data store. I am not sure if I am taking the best approach to testing it. - (IBAction)add:(id)sender { NSString *itemName = [self.itemNameTextField stringValue]; SGItem *newItem = [NSEntityDescription insertNewObjectForEntityForName:kItemEntityName inManagedObjectContext:[self managedObjectContext]]; newItem.name = itemName; NSError *error = nil; BOOL canSaveNewItem = [[self

EXC_BAD_ACCESS when accessing parameters in andDo of OCMock

血红的双手。 提交于 2019-12-03 16:14:26
问题 I am trying to write an block of code using OCMock's stub andDo method. In this case UIImageView extension class is being tested. I want to check that the extension calls [self setImage:] with parameter that is non-nil (later other image comparison will be used). When using OCMock's andDo method, the test crashes with EXC_BAD_ACCESS after the block completes. id mockView = [OCMockObject mockForClass:[UIImageView class]]; [[[mockView stub] andDo:^(NSInvocation *invocation) { UIImage *img;

OCMock and block testing, executing

╄→尐↘猪︶ㄣ 提交于 2019-12-03 13:18:58
问题 Here's the method under test: - (void)loginWithUser:(NSString *)userName andPass:(NSString *)pass { NSDictionary *userPassD = @{@"user":userName, @"pass":pass}; [_loginCntrl loginWithUserPass:userPassD withSuccess:^(NSString *authToken){ // save authToken to credential store } failure:^(NSString *errorMessage) { // alert user pass was wrong }]; } what I want to test is that in that success block the other dependency/OCMockObject _credStore is called with the appropriate methods. So currently

How do i mock a method that accepts a handle as an argument in OCMock?

早过忘川 提交于 2019-12-03 11:03:09
I'm trying to mock a method that has the equivalent of the following signature: - (NSDictionary *) uploadValues:(BOOL)doSomething error:(NSError **)error I want it to return a small dictionary so that my test can make sure the code uses the dictionary properly. however, no matter what i do OCMock always returns nil from the method, regardless of how i stub it out. The error begins as nil in the code i'm testing, and these are the different ways i've tried stubbing it: NSError * error = nil; [[[mock stub] andReturn:someDict] uploadValues:YES error:&error]; [[[mock stub] andReturn:someDict]

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

[亡魂溺海] 提交于 2019-12-03 10:13:38
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, I have another similar function which dispatch block to an sequential queue other than main queue: -

Passing primitives to an OCMock's stub

℡╲_俬逩灬. 提交于 2019-12-03 09:26:28
I'm learning how to use OCMock to test my iPhone's project and I have this scenario: a HeightMap class with a getHeightAtX:andY: method, and a Render class using HeightMap . I'm trying to unit test Render using some HeightMap mocks. This works: id mock = [OCMockObject mockForClass:[Chunk class]]; int h = 0; [[[mock stub] andReturnValue:OCMOCK_VALUE(h)] getHeightAtX:0 andY:0]; Of course, works only for x=0 and y=0 . I want to test using a "flat" height map. This means I need to do something like this: id chunk = [OCMockObject mockForClass:[Chunk class]]; int h = 0; [[[chunk stub] andReturnValue

EXC_BAD_ACCESS when accessing parameters in andDo of OCMock

不打扰是莪最后的温柔 提交于 2019-12-03 05:26:56
I am trying to write an block of code using OCMock's stub andDo method. In this case UIImageView extension class is being tested. I want to check that the extension calls [self setImage:] with parameter that is non-nil (later other image comparison will be used). When using OCMock's andDo method, the test crashes with EXC_BAD_ACCESS after the block completes. id mockView = [OCMockObject mockForClass:[UIImageView class]]; [[[mockView stub] andDo:^(NSInvocation *invocation) { UIImage *img; [invocation getArgument:&img atIndex:2]; <---- line causing the exception somebodySetImage |= (img != nil);

How can I use OCMock to verify that a method is never called?

余生长醉 提交于 2019-12-03 04:17:35
At my day job I've been spoiled with Mockito's never() verification , which can confirm that a mock method is never called. Is there some way to accomplish the same thing using Objective-C and OCMock? I've been using the code below, which works but it feels like a hack. I'm hoping there's a better way... - (void)testSomeMethodIsNeverCalled { id mock = [OCMockObject mockForClass:[MyObject class]]; [[[mock stub] andCall:@selector(fail) onObject:self] forbiddenMethod]; // more test things here, which hopefully // never call [mock forbiddenMethod]... } - (void)fail { STFail(@"This method is