Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock Object not able to stub dynamic properties #218

Closed
jhoughjr opened this issue Aug 6, 2015 · 10 comments
Closed

Mock Object not able to stub dynamic properties #218

jhoughjr opened this issue Aug 6, 2015 · 10 comments

Comments

@jhoughjr
Copy link

jhoughjr commented Aug 6, 2015

I'm mocking UILocalNotification.
In its header, it declares :
(line 47, UILocalNotification.h)
@Property(nonatomic,copy) NSDictionary *userInfo; // throws if contains non-property list types

However expecting -userInfo on the UILocalNotification mock throws the "cannot stub/verify .. because it does not exist in the mocked class" exception.

If I do not expect it, when the test runs I get an unrecognized selector exception.

I dont understand why this is happening since it appears to be the same scenario in many tests that work fine.

Here is the test and code under test that reproduces this scenario
https://gist.github.com/jhoughjr/dcfd9a7bbf869c0695d1

Possibly I am doing something wrong, but I don't see it, as this seems a simple test case.

@d-ronnqvist
Copy link

The same problem seem to happen with other properties (I'm stubbing taskDescription of NSURLSessionDataTask).

It looks like it has something to do with the instance method signature being nil:

[NSURLSessionDataTask instanceMethodSignatureForSelector:@selector(setTaskDescription:)]

returns nil for me (in Xcode 7)

@erikdoe
Copy link
Owner

erikdoe commented Aug 19, 2015

So, if the class cannot provide a method signature, it's somewhat tricky for OCMock to mock the method. What happens when you mock the valueForKey: method instead? The discussion on #68 could also be helpful.

@erikdoe
Copy link
Owner

erikdoe commented Aug 22, 2015

I have just merged #223 which makes it possible to stub dynamic properties as if they were regular methods. Can you verify that this fixes your problem?

@d-ronnqvist
Copy link

It does not resolve the issue I was facing (on Xcode 7). That issue can very concisely be verified with this test:

- (void)testCanStubTaskDescriptionOnNSRULSessionTask
{
    id mock = [OCMockObject mockForClass:[NSURLSessionDataTask class]];
    [[mock stub] setTaskDescription:OCMOCK_ANY]; // stubbing setTaskDescription: fails
    [mock setTaskDescription:@"test-value"];
}

Should I file a separate issue for that?

@erikdoe
Copy link
Owner

erikdoe commented Aug 27, 2015

Yes, I see. The change in #223 only provides the getters while you need the setter.

@jhoughjr
Copy link
Author

jhoughjr commented Sep 1, 2015

I'll update my pod and check later today. seems i found some workaround.
Never mind this previous comment. Xcode completed the wrong method it seems.

@imhuntingwabbits
Copy link
Contributor

Sorry for the delay, got busy at work but I'll take a look at this tonight. Mocking the setter should just require synthesizing the method signature in the same way as the getter.

@imhuntingwabbits
Copy link
Contributor

#240

@erikdoe erikdoe changed the title Mock Object not recognizing a selector that is defined in the mocked class Mock Object not able to stub dynamic properties Sep 20, 2015
@erikdoe
Copy link
Owner

erikdoe commented Oct 3, 2015

Assuming fixed.

@erikdoe erikdoe closed this as completed Oct 3, 2015
@imhuntingwabbits
Copy link
Contributor

I think so given you merged my PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants