Questions tagged [ocmockito]

OCMockito is an Objective-C implementation of Mockito, supporting creation, verification and stubbing of mock objects.

Key differences from other mocking frameworks:

  • Mock objects are always "nice," recording their calls instead of throwing exceptions about unspecified invocations. This makes tests less fragile.
  • No expect-run-verify, making tests more readable. Mock objects record their calls, then you verify the methods you want.
  • Verification failures are reported as unit test failures, identifying specific lines instead of throwing exceptions. This makes it easier to identify failures. (It also keeps the pre-iOS 5 Simulator from crashing.)
70 questions
3
votes
1 answer

Is there a workaround for the lack of a verify method on Kiwi mocks?

I have a strong preference for a highly predictable Arrange Act Assert format for unit tests. Because Kiwi doesn't have an explicit verify statement for mocks, it forces a different pattern, something like: // Arrange Thing *mockThing =…
Cris
  • 1,939
  • 3
  • 23
  • 37
2
votes
1 answer

OCMockito stub singleton shared instance method

I was using OCMock for stubbing class lvl methods and it worked well. Now I need to achieve the same behaviour with OCMockito. In OCMock I have following: _mock = mockClass([MySingleton class]); OCMStub([_mock…
Skodik.o
  • 506
  • 4
  • 21
2
votes
1 answer

Unit test case for call back methods ios

I have a following method in my app for which I need to write unit test cases. Can anyone suggest how can I test whether the success block or error block is called. - (IBAction)loginButtonTapped:(id)sender { void (^SuccessBlock)(id,…
Shubham
  • 88
  • 6
2
votes
1 answer

Mock internal calls of a another class methods

I am trying to learn unit testing with ocmock. I am finding it difficult to mock calls of another class from a class which i am unit testing. Can some one suggest how to make mock call to KeyChainUtils class and HttpRequest class: Code to unit test…
Rajeev
  • 4,762
  • 8
  • 41
  • 63
2
votes
1 answer

How do I stub a class method with OCMockito?

The OCMockito documentation claims it's possible to mock class objects, but I'm damned if I can work out how. The following test fails with Expected "Purple" but was "": - (void)testClassMethodMocking { Class stringClassMock =…
Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
2
votes
1 answer

Can OCMockito stub a method that takes a `const void *` parameter?

My MessageSerializer class has a method whose signature looks like this: - (Message *)deserialize:(const void *)buffer length:(NSUInteger)length; Can I use OCMockito to stub it? Where serializer is my mock serializer, the compiler approves of all…
Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
2
votes
1 answer

OCMockito - mocked class with "willReturn" returns nil rather than the value I specify

The code I'm working with looks something like this:- - (MyOrange *) getAnOrangeFromBowl:(MyBowl *)bowl withSize:(NSString *)size { MyOrange *orange = [bowl getAnOrangeWithSize:size]; return orange; } Before you ask, no I can't just call my…
Andrew Sumner
  • 323
  • 3
  • 8
2
votes
1 answer

OCMockito crash when mocking KVO observer

I'm trying to mock an object that is passed to my SUT. When passed, the SUT registers the mock as observer for some properties. In SUT dealloc, it calls removeObserver on the mock. This was working just fine with OCMockito 0.23, but when updating to…
2
votes
2 answers

Is there an idiomatic Objective-C technique to make an init method return a stub implementation for testing?

I am writing a unit test for a class Foo, which has a collaborator, Bar. I want to use a manually-built stub implementation of Bar in Foo's test. If I were doing this in Java, I would give Foo a BarFactory collaborator and inject a MockBarFactory in…
Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
2
votes
1 answer

Verify method call with OCMockito

I have these two methods in a ClassA -(IBAction)onSubmit; -(void)validateName:(NSString*)name; @Implementation - (IBAction)onSubmit { [self validateName:self.textfield.text]; } -(void)validateName:(NSString*)name{ // do something } My…
Bach
  • 2,684
  • 5
  • 26
  • 36
2
votes
1 answer

How to set expectations on parameters to mocked methods in Kiwi

Using OCMockito and OCHamcrest, I can set up expectations on the arguments to mocked methods, thusly: [verify(aMockObject) doSomething:allOf(is(instanceOf([NSArray class])), hasCountOf(3U), nil)]; There doesn't seem to be an equivalently simple way…
Cris
  • 1,939
  • 3
  • 23
  • 37
1
vote
1 answer

OCMockito: RACObserve a mocked object always fail

code like this: @implementation MyClass - (void)func { //MyOtherClassObject is an object passed in when initialising MyClass NSArray *signals = @[[RACObserve(MyOtherClassObject, prop) subscribeNext:^{{}]]; } @end @implementation MyTest -…
CarmeloS
  • 7,868
  • 8
  • 56
  • 103
1
vote
0 answers

How can I test asynchronous delegate methods using Expecta/OCMockito?

I'm currently using Kiwi to write tests, but I'm interested in trying out Specta/Expecta/OCMockito (or another mocking library, if necessary). One thing I use Kiwi for is testing that delegate methods are called after some asynchronous work. For…
MaxGabriel
  • 7,617
  • 4
  • 35
  • 82
1
vote
1 answer

OCMockito - Verify order of method invocations

As far as I can see, there is no way to verify the order of method invocations on a mock. Or am I missing something? - (void)testResetCameraState_resetsCameraView { // Arrange [given([_cameraManagerMock previewLayer]) willReturn:_testLayer]; …
florieger
  • 1,310
  • 12
  • 22
1
vote
1 answer

Verify that correct frame is set on subview

Intro I'd like to test that the right frame is set on a views subview (which is injected as a mock). However I can't seem to find a way to verify that the right CGRect is set Current Attempt -(void)testThatCorrectFrameIsSet { //Arrange …
Menno
  • 1,232
  • 12
  • 23