ocmock

Check argument value passed into function in unit test

為{幸葍}努か 提交于 2019-12-08 04:39:21
问题 I am using OCMock v3 do unit testing, I want to test the following piece of code: @implementation School -(void) handleStudent:(Student*) student{ Bool result = [self checkIdentityWithName:student.name age:student.age]; ... } ... @end In my following test case I created a student instance with name "John", age 23. and then I run the function under test: -(void) testHandleStudent{ Student *student = [Student initWithName:@"John" age:23]; // function under test [schoolPartialMock handleStudent

OCMock: stub a @dynamic property

我的未来我决定 提交于 2019-12-08 01:14:43
问题 I'm trying to add unit tests to an existing iOS application, using among others OCMock . In this application, we have a bunch of CoreData entities and generated classes. These classes obviously contain @dynamic properties. I tried to stub one of these properties as follows: self.event = [OCMockObject mockForClass:[ACEvent class]]; [[[self.event stub] andReturn:@"e46e1555-d866-4160-9b42-36d0fb9c29cd"] eventGUID]; The point is, it doesn't work. Apparently because an @dynamic property does not

How can you test the contents of a UIAlertAction handler with OCMock

眉间皱痕 提交于 2019-12-07 14:05:07
问题 I've got an app where I push a UIAlertController with several custom UIAlertAction s. Each UIAlertAction performs unique tasks in the handler block of actionWithTitle:style:handler: . I have several methods that I need to verify are executed within these blocks. How can I execute the handler block so that I can verify that these methods are executed? 回答1: With a bit of clever casting, I've found a way to do this in Swift (2.2): extension UIAlertController { typealias AlertHandler =

OCMock and Blocks

陌路散爱 提交于 2019-12-07 12:54:22
问题 I have a method with the following signature that I want to test using OCMock's stub feature: - (void)signInWithEmail:(NSString *)email andWithPassword:(NSString *)password andWithBlock:(void (^)(GNCustomer *customer, NSError *error))block How would I go about mocking this to handle the returned block. I have tried: [[_mockAuthenticationRepository stub] signInWithEmail:[OCMArg any] andWithPassword:[OCMArg any] andWithBlock:^(GNCustomer *customer, NSError *error) { }]; When I try to stub with

iOS - Unit Testing Asynchoronous code

对着背影说爱祢 提交于 2019-12-07 10:28:51
问题 The part of a method that I am trying to test is as follows: - (void)configureTableFooterView { dispatch_async(dispatch_get_main_queue(), ^{ self.tableView.tableFooterView = nil; if ([self.parser.resultSet isLastPage]) { return; } }); } I have written the unit test as follows: - (void)testTableFooterViewConfigurationAfterLastPageLoaded { id mockTableView = OCMClassMock([GMGFlatTableView class]); OCMExpect([mockTableView setTableFooterView:[OCMArg isNil]]); id resultSet = OCMClassMock(

stub method with block of code as parameter with OCMock

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 06:04:43
问题 Is there a way to stub method, that takes block as it's parameter? For example mehod: - (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler; 回答1: Yes. The easiest way would be to accept anything: id mockGeocoder = [OCMockObject mockForClass:[CLGeocoder class]]; [[mockGeocoder stub] reverseGeocodeLocation:[OCMOCK_ANY] completionHandler:[OCMOCK_ANY]]; It gets a bit trickier if you want to verify a particular block is passed in. One

Objective C & OC Mock - Mocking a class method?

允我心安 提交于 2019-12-06 04:23:04
问题 I need to be able to determine whether a class method was called or not. How can I do this with OCMock? 回答1: Starting with OCMock release 2.1 this is supported out of the box. You can now stub class methods in the same way as you stub instance methods. 回答2: One approach is to wrap the class method in a method on your own class. So let's say your class has to call [SomeOtherClass classMethod:someString] . You could create a method invokeClassMethod: on your class like this: -(NSString *

How can you test the contents of a UIAlertAction handler with OCMock

三世轮回 提交于 2019-12-05 19:03:10
I've got an app where I push a UIAlertController with several custom UIAlertAction s. Each UIAlertAction performs unique tasks in the handler block of actionWithTitle:style:handler: . I have several methods that I need to verify are executed within these blocks. How can I execute the handler block so that I can verify that these methods are executed? With a bit of clever casting, I've found a way to do this in Swift (2.2): extension UIAlertController { typealias AlertHandler = @convention(block) (UIAlertAction) -> Void func tapButtonAtIndex(index: Int) { let block = actions[index].valueForKey(

iOS - Unit Testing Asynchoronous code

佐手、 提交于 2019-12-05 16:29:33
The part of a method that I am trying to test is as follows: - (void)configureTableFooterView { dispatch_async(dispatch_get_main_queue(), ^{ self.tableView.tableFooterView = nil; if ([self.parser.resultSet isLastPage]) { return; } }); } I have written the unit test as follows: - (void)testTableFooterViewConfigurationAfterLastPageLoaded { id mockTableView = OCMClassMock([GMGFlatTableView class]); OCMExpect([mockTableView setTableFooterView:[OCMArg isNil]]); id resultSet = OCMClassMock([GMGResultSetInfo class]); OCMStub([resultSet isLastPage]).andReturn(YES); OCMStub([self.mockParser resultSet])

cannot stub class method with OCMock 2.1+ in Xcode 5.0

蹲街弑〆低调 提交于 2019-12-05 16:10:16
I know that OCMock version 2.1+ supports stubbing class methods out of the box. But for some reason it's not working with me. To make sure I isolated the problem, I simply cloned the example OCMock project (which is clearly marked as version 2.2.1) and simply added this inside testMasterViewControllerDeletesItemsFromTableView : id detailViewMock = [OCMockObject mockForClass:[DetailViewController class]]; [[[detailViewMock stub] andReturn:@"hello"] helloWorld]; in DetailViewController.h I added: + (NSString *)helloWorld; and DetailViewController.m : + (NSString *)helloWorld { return @"hello