Mock using implicit valueForKeyPath?

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Mock using implicit valueForKeyPath?

Postby itsthejb » 19 Sep 2012, 15:27

Hi everyone,

First post, so just like to say I've been using OCMock for some years now and really appreciate the great work done here. Keep it up!

Query/feature suggestion coming up;

I've had a lot of pain over the years setting up testing states on objects that have a lot of sub-properties; creating mocks for each, stubbing the right properties, stubbing sub-mock objects, etc. etc. Let me illustrate in pseduo-code;

Code: Select all
@class A
@property () FooProperty *foo

FooProperty *testFoo = [OCMockObject mockForClass:[FooProperty class]];
A *testObject = [[A alloc] init]; = testFoo;

Now imagine that FooProperty has a big hierarchy and class A can do things like;

Code: Select all
id name =;

It would be wonderful if I could set up this state with something like;

Code: Select all
[[[testFoo stub] andReturn:@"Bar"] forKeyPath:@""]

This doesn't seem to work at the moment because A doesn't explicitly use valueForKeyPath, such as;

Code: Select all
id name = [ valueForKeyPath:@""]

Can this already be done? If not, it doesn't seem like it would be too challenging to add - something like using -valueForUndefinedKeyPath?

I hope my description is understandable! Thanks for your help!
Posts: 1
Joined: 19 Sep 2012, 15:13

Return to OCMock