Questions tagged [ocmock]

OCMock is an Objective-C implementation of mock objects.

The OCMock framework provides the ability of using mock objects in Objective-C applications. Some of the functionality in the framework is:

  • Create fail-fast and nice mocks.
  • Ability to stub and return predetermined values.
  • Mock instance and class methods.
  • Mock protocols and notification observers.
  • Partial mocks to stand in for a real object.
  • Forward messages to other objects.

Relevant links

Web site

Source

484 questions
4
votes
2 answers

OCMock test an object is allocated and a method is called on it

I'm finally imposing some TDD on a project I'm working on, and running into the edges... I know the code I want but not how to test for it :) The implementation I'm looking for is: - (void) doSomething { FooBuilder *foo = [[FooBuilder alloc]…
dpassage
  • 5,423
  • 3
  • 25
  • 53
4
votes
0 answers

NSInvocation getArgument:atIndex: confusion while testing blocks with OCMock

I'm writing unit tests to my Facebook SDK wrapper and something got me confused about NSInvocation's - (void)getArgument:(void *)buffer atIndex:(NSInteger)index while trying to mock FBRequest with OCMock. Here's the method definition I'm trying to…
Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41
4
votes
1 answer

Why does OCMock partialMock break KVO?

If I have an object that uses KVO to observe a property on some object and then create a partial mock for that observer I no longer receive any notifications. Why is this? Here's a minimal example: @interface TestPartialMockAndKVO :…
Andreas Järliden
  • 1,884
  • 1
  • 17
  • 16
4
votes
1 answer

OCMock returning values

I'm trying to write a test for a method where the output depends on an NSDate's timeIntervalSinceNow return value. I'd like to specify the return value in my tests so I can test certain scenarios. I'm having a really hard time getting this OCMock…
Doug P
  • 361
  • 1
  • 4
  • 15
4
votes
1 answer

Mocking a pointer to a pointer in Objective-C

I'm trying to mock a call to NSURLConnection. I swizzle the class method call with my own that calls my mock object. I'm trying to implement the mock like this: [[[[urlConnectionMock expect] andDo:^(NSInvocation *invocation) { NSURLResponse *…
Ben Flynn
  • 18,524
  • 20
  • 97
  • 142
4
votes
2 answers

Test a method that has been refactored (split up into multiple methods)?

As I heard before and recently learned in Jon Reid's very good screencast, some init methods or in iOS applications, the viewDidLoad methods tend to get bigger and bigger. I tried to refactor this method: - (void)viewDidLoad { // 10+ lines of…
Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
4
votes
1 answer

Use OCMock to mock a block expectation

I'm new to OCMock and I'm planing to mock a request call. The API that needs to be mocked is executed as defined below. [ProductRequest requestProductUpdateUrl: @"testUrl" withParameters:params error:^(NSString *updateUrl, NSError *error){ …
Jani
  • 1,400
  • 17
  • 36
4
votes
2 answers

How to cast from id __autoreleasing * to void ** in ARC?

I'm trying to cast from an id __autoreleasing * to a CFTypeRef * (void **). I've tried: id __autoreleasing *arg = [OCMArg setTo:mockData]; CFTypeRef expectedResult = (__bridge CFTypeRef) *arg; [[[self.mockSecItemService expect]…
Eric
  • 2,045
  • 17
  • 24
4
votes
1 answer

OCMock to test passing block is execute correctly

How can I verify that passing block is execute correctly ? - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [self updatePostalCode:newLocation…
sarunw
  • 8,036
  • 11
  • 48
  • 84
3
votes
1 answer

IPhone unit testing OCMock, how to mock read only variables?

We always use OCMock in the following way, but it seems doesn't work for some read only property variables, such as NavigationController and so forth, it is read only, set mock one doesn't take effect. Have you ever experienced this tough issue,…
jianhua
  • 1,011
  • 1
  • 12
  • 28
3
votes
1 answer

Obj-C: How to get and call a block argument from NSInvocation - stubbing Twitter account on iOS

I'm testing an iOS application using KIF and OCMock, stubbing the device's ACAccountStore to return my own representation of a Twitter account. I want to stub requestAccessToAccountsWithType, and call the passed completion handler with my own…
Jonathan Julian
  • 12,163
  • 2
  • 42
  • 48
3
votes
1 answer

Are there any test spy libraries available for Objective-C?

I want to take a BDD approach to unit testing in an iOS project, and I just realized that there may not be an existing library that provides test doubles of the test spy variety. Ideally, I'm looking for something similar to Mockito, Jasmine, or…
Justin Searls
  • 4,789
  • 4
  • 45
  • 56
3
votes
1 answer

in unit test, set mocked value to an argument (pass-by-reference)

I have a method: -(void)startTaskForResult:(long long*)result { ... } The function I want to unit test invoke above function: -(void)doWork { long long result = 0; [self startTaskForResult:&result]; } I am using OCMock library to do unit…
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
3
votes
1 answer

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

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…
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
3
votes
3 answers

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

I've got an app where I push a UIAlertController with several custom UIAlertActions. 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…
Drew H
  • 1,226
  • 1
  • 12
  • 13