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
3
votes
2 answers

Mocking class methods with OCMock not working

What's wrong with this code? Trying to make a class method return @NO, but it's returning @YES (the NSLog prints "1"). // Make the test think you don't have any internet id mockFunctions = [OCMockObject mockForClass:[Functions…
Luke The Obscure
  • 1,524
  • 2
  • 20
  • 35
3
votes
2 answers

OCMock stub for method's pass-by-reference argument

I have a method that I need to stub. The method is of the form below: BOOL myMethodWithError:(*__autoreleasing *NSError)error; So I mocked the object and attempted to return a nil back through 'error'. I coded it as follows. id mockMyObject =…
Rob
  • 4,149
  • 5
  • 34
  • 48
3
votes
1 answer

OCMock on a method with argument and returns a value

I have a class that relies on NSUserDefaults that I'm trying to unit-test and I'm providing the NSUserDefaults as a mock into my test class. When running the test, I get the error: OCMockObject[NSUserDefaults]: unexpected method invoked: …
Alexi Groove
  • 6,646
  • 12
  • 47
  • 54
3
votes
3 answers

OCMock - partial mocking [UIAlertView alloc]

I'm having an issue with the OCMock framework for iOS. I'm essentially trying to mock UIAlertView's initWithTitle:message:delegate... method. The example below doesn't work in the sense that the stubbed return value isn't returned when I call the…
larromba
  • 306
  • 2
  • 11
3
votes
1 answer

OCMock and Blocks

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…
strickland
  • 1,943
  • 5
  • 21
  • 40
3
votes
1 answer

Unable to mock class method on CLLocationManager using OCMock with iOS7

I'm trying to write an XCTest (iOS7, XCode5) together with OCMock. I've got a class which implements the CLLocationManagerDelegate protocol, and has a property which is an instance of a CLLocationManager. (I supply the instance of the…
3
votes
2 answers

How to test async method in block using OCMock

I can't seem to figure out how to test this method: - (void)writer:(id)writer didFailWithError:(NSError *)error; { [self.alertView dismissWithClickedButtonIndex:0 animated:YES]; void (^alertViewBlock)(int) = ^(int buttonIndex) { if…
jimijon
  • 2,046
  • 1
  • 20
  • 39
3
votes
2 answers

OCMock: stub a @dynamic property

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…
aspyct
  • 3,625
  • 7
  • 36
  • 61
3
votes
5 answers

OcMock - Stub / expect / stopmocking

in my fixture setUp i have the following -(void)setUp{ _vc = [OCMockObject partialMockForObject:[[InboxViewController alloc] init]]; //stub out the view stuff [[_vc stub] removeTask:OCMOCK_ANY]; [[_vc stub]…
Zayin Krige
  • 3,229
  • 1
  • 35
  • 34
3
votes
1 answer

OCMock expect a method called within another method

This is an extremely basic question about OCMock expectations. Let's you have an instance method methodA on objectA that calls an instance method methodB on objectA. - (void)methodA { [self methodB]; } - (void)methodB { ... } Now, let's…
Rahul Jaswa
  • 509
  • 4
  • 17
3
votes
1 answer

How to properly stub methods on a different thread using OCMock?

I have the following unit test that is failing. I think its because OCMock does not work well across multiple threads but I could be wrong. mockTestMethodA never gets called. If I modified the code to call the testMethodA on the same thread (without…
3
votes
2 answers

How can I test addObserver and removeObserver are invoked on nsnotificationcenter, using ocmock

in my base mock class: - (void)tearDown { _mockApplication = nil; self.observerMock = nil; self.notificationCenterMock = nil; } where notificaitonCenterMock is just an id; Then in my tests I do things like…
Infrid
  • 336
  • 1
  • 3
  • 13
3
votes
2 answers

OCMock Argument Match on Double Pointer

For a selector like this: -(void) callFoo:(NSError**)error; How can I get an expectation like the below to work? ARC is not liking this kind of expectation, and I don't want to disable ARC for the entire unit test file! [[mockObject expect]…
Ray
  • 35
  • 4
3
votes
2 answers

Compiler confused about mock object

I'm using OCMock to aid in Test Driven Development I am doing for an iPad application using Xcode. I have test code like this: id mock = [OCMockObject mockForProtocol:@protocol(SomeProtocol)]; Vector direction = { 1.0f, 2.0f, 3.0f }; [[mock expect]…
Tron Thomas
  • 871
  • 7
  • 20
3
votes
2 answers

Mock a class whose methods are invoked in the test case

So I have a class I wrote some test cases for. This class has these two methods: - (void)showNextNewsItem { self.xmlUrl = self.nextNewsUrl; [self loadWebViewContent]; } - (void)showPreviousNewsItem { self.xmlUrl = self.previousNewsUrl; …
Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58