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

Confusion with nullMock and addObserver:... #371

Open
dmeehan1968 opened this issue Sep 16, 2013 · 4 comments
Open

Confusion with nullMock and addObserver:... #371

dmeehan1968 opened this issue Sep 16, 2013 · 4 comments

Comments

@dmeehan1968
Copy link

It's been a few months since I've had a chance to do much with Kiwi, but now getting back into it, and can't seem to make sense of the following. Am I doing something drastically wrong?

I'm trying to write a test that validates that an object has an observer added to it. in my code, I'm trying to inject a nullMock into the class being tested so that I can check that the KVO calls are being made. I started with a real instance of the observed class and this was working ok, but the automatic stubbing was causing problems elsewhere, so I wanted to adapt the tests to use mocks to simplify things.

The code below is a example of this behaviour, not from my project, but the effect is the same.

When observed is a nullMock, the addObserver expectation fails, but the addObject is correctly ignored.

When observed is a mock, the expectation passes, but then I get a failure because of the unexpected addObject (which is correct behaviour for Kiwi).

So the problem appears to be that Kiwi can't set an expectation on addObserver:forKeyPath:options:context: when using nullMocks.

NSMutableArray *observed = [NSMutableArray nullMock];
NSObject *observer = [NSObject nullMock];

[[observed should] receive:@selector(addObserver:forKeyPath:options:context:)];

[observed addObserver: observer forKeyPath:@"count" options: NSKeyValueObservingOptionNew context: nil];

[observed addObject:@1];

I get the error:

expected subject to receive -addObserver:forKeyPath:options:context: exactly 1 time, but received it 0 times

I'm using :head in my Podfile to get the latest Kiwi.

@modocache
Copy link
Member

I believe you've exposed a bug in Kiwi's implementation. Namely, the following test fails, although I believe it should succeed:

it(@"asserts a null mock receives -addObserver:forKeyPath:options:context:", ^{
    NSObject *nullMock = [NSObject nullMock];
    [[nullMock should] receive:@selector(addObserver:forKeyPath:options:context:)];
    [nullMock addObserver:[NSObject new]
               forKeyPath:@"description"
                  options:0
                  context:nil];
});

I'll try looking into it.

@dmeehan1968
Copy link
Author

I've worked with Kiwi mocks for KVO in the past and they seemed fine, but the nullMock is I think ignoring more than it should do. Instance methods on NSObject should probably be allowed to pass through.

@modocache
Copy link
Member

The weird thing is that most instance methods do work. The following test passes, as expected:

it(@"asserts a null mock receives -description", ^{
    NSObject *nullMock = [NSObject nullMock];
    [[nullMock should] receive:@selector(description)];
    [nullMock description];
});

I believe the problem is specific to -addObserver:forKeyPath:options:context:.

@tkuichooseyou
Copy link

This issue comes up for me even with regular mock and not just nullMock. I realize this issue is old, sorry to disturb the peace :(

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

No branches or pull requests

3 participants