質問

I made a subclass of MKMapView. In unit test, I want to know when I invoke a certain method of this subclass, that a corresponding MKMapView method is invoked and the class of the argument. Can I use OCMock to accomplish this?

EDIT:

I accepted the answer, but I want to clarify that my question was on when the super class cannot be directly stand-in in the unit test (obviously so, because that's how inheritance works). And it seems like both OCMock and OCMockito can only be used for objects that are injecting into the class under the test. So basically, I think I need to resort to manually swizzle them.

役に立ちましたか?

解決

To answer this question, let's spend a little bit of time to look at how OCMock and similar frameworks work:

Mocking frameworks use the Objective-C runtime to proxy or wrap the original class, so that any invocation of a method on that class is redirected to the mocking framework, which has a pre-recorded set of instructions about what to do when a given method is invoked.

Therefore, the answer is, yes, when you mock a class, you are also implicitly mocking the methods on the super class. You can record an expectation that this method will be invoked and verify that this happened. You can also verify the arguments that were supplied.

It is not necessary to know the details in order to effectively use a mocking framework, however if you're interested in exploring some approaches to proxying a class, take a look at the following:

Incidentally, my personal favorite mocking framework is OCMockito

他のヒント

I don't think OCMock can accomplish this.

See How to Test Calls to Super (iosunittesting.com)

I found the above linked post to be very helpful with this common issue. What I ended up doing was creating a category on the superclass (only in my testing target) which overrides the method in question and sets some global state that can be inspected by a test case (approach #1 in the post).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top