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
5
votes
1 answer

XCTest fails when calling [NSBundle mainBundle]

I have some code that calls [NSBundle mainBundle] at some point, mainly to read/set preferences. When I unit test the method, the test fails because the test's mainBundle does not contain the file. This is a known issue, that Apple won't fix as…
KPM
  • 10,558
  • 3
  • 45
  • 66
5
votes
3 answers

Mocking C library functions in Objective-C

I'm a beginner to Objective-C, but I've used jUnit a fair bit. For unit testing my code, I need to be able to mock the network, which I'm using through the CFStreamCreatePairWithSocketToHost function. Per the answer to How to mock a C-function using…
Brennan Vincent
  • 10,736
  • 9
  • 32
  • 54
5
votes
1 answer

Returning an NSString from an NSInvocation using setReturnValue

When I set the return value of an NSInvocation to be an NSString, the invoker is receiving an NSCFString. In my case I'm mocking to pull a bundle path from file included by unit tests: [[[_bundlePartial stub] andDo:^(NSInvocation *invocation) { …
Ben Flynn
  • 18,524
  • 20
  • 97
  • 142
5
votes
0 answers

OCMock is not cleaning up stubbed class methods

I'm trying to use OCMock to mock out internal class dependencies (e.g. my class uses an NSMutableData and verifying against a mock NSMutableData). Specifically I'm mocking factory methods to return mock objects. As far as I can tell the mock objects…
Brandon
  • 113
  • 5
5
votes
1 answer

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

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…
Nico Prananta
  • 474
  • 4
  • 14
5
votes
1 answer

Failing to import OCMock headers into iOS project

I'm trying to add OCMock to my current project using the instructions at ocmock.org. Added the library and the headers to the project. Made sure that the libOCMock.a is added to my test target. Added the search path to the header files in Header…
ABeanSits
  • 1,725
  • 1
  • 17
  • 34
5
votes
1 answer

Unit Tests are always "successful" after adding OCMock

I would like to use OCMock in my Cocoa project's unit tests. I have downloaded the latest version of OCMock (2.0.1) and copied the OCMock.framework into my project's directory. In Xcode, I have added the framework to the unit test target's link…
waldrumpus
  • 2,540
  • 18
  • 44
5
votes
1 answer

stub method with block of code as parameter with OCMock

Is there a way to stub method, that takes block as it's parameter? For example mehod: - (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
kraag22
  • 3,340
  • 3
  • 29
  • 37
5
votes
2 answers

OCMock an NSOperation

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…
Nick Cipollina
  • 501
  • 3
  • 13
5
votes
1 answer

Issue running iOS unit tests on command line (OCUnit + OCMock)

I'm trying to get unit tests to run in CI (Jenkins). I came across this a article online with some hacks to get tests running in command line. I tried it on an empty project with the default setup for unit tests and the instructions there work just…
4
votes
3 answers

Mocking KVO with OCMock

I would like to test that Key-Value-Observation is working properly for a class of mine. It has one property, which depends on another. They are set up like so: + (NSSet *)keyPathsForValuesAffectingSecondProperty { return [NSSet setWithObjects: …
Dov
  • 15,530
  • 13
  • 76
  • 177
4
votes
4 answers

How to use Core Data for Dependency Injection

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…
Cris
  • 1,939
  • 3
  • 23
  • 37
4
votes
4 answers

Objective C & OC Mock - Mocking a class method?

I need to be able to determine whether a class method was called or not. How can I do this with OCMock?
aryaxt
  • 76,198
  • 92
  • 293
  • 442
4
votes
1 answer

Using block expectations with OCMock

I am using GHUnit & OCMock to do some testing work in my iOS app. So I have some trouble integrating them. The following code works well. NSString *s = [NSString stringWithString:@"122"]; id mock = [OCMockObject partialMockForObject:s]; [[[mock…
4
votes
1 answer

OCMock asynchronous block callback

I am building a small library to handle file upload and download operations for me and am trying to integrate a suite of tests into it. Rather than using delegate callback methods, I am handling the asynchronous responses in a completion handler…
Justin Williams
  • 703
  • 1
  • 9
  • 26