Cruinh
Cruinh

Reputation: 3721

Using OCMockito how can one verify any selector was passed to a method

I have a view controller that adds itself as an observer of UIApplicationDidBecomeActiveNotification during viewDidLoad. I'd like to verify that this happens but I don't want the test to care what specific selector the view controller registers for the event.

Currently my test looks something like this:

- (void)testRegistersForApplicationDidBecomeActiveEvent
{
   //given
   MyViewController *sut = [MyViewController new];
   NSNotificationCenter* mockNotificationCenter = mock([NSNotificationCenter class);

   //when
   [sut view];

   //then
   [verify([mockNotificationCenter]) addObserver:sut
                                        selector:anything()
                                            name:UIApplicationDidBecomeActiveNotification
                                          object:nil];
}

...but passing "anything()" for the selector gives a compiler error: "Implicit conversion of an Objective-C pointer to 'SEL' is disallowed with ARC".

I can make the test work if I pass "@selector(applicationDidBecomeActive:)" instead of anything. That is the exact selector the view controller uses. But I'd prefer the test not have that much knowledge of the specific implementation, if possible.

Upvotes: 3

Views: 297

Answers (1)

Eugen Martynov
Eugen Martynov

Reputation: 20140

anything() is applicable only for id (from the OCHamcrest API) which states for an object and selector states for a method.

I would raise issue on the GitHub and right now provide extra knowledge in test with specifying exact.

I think it should be quite easy contribution (since providing exact selector is working for test)

Upvotes: 1

Related Questions