specta / expecta

A Matcher Framework for Objective-C/Cocoa
MIT License
1.59k stars 158 forks source link

expecting notifications not working? #163

Open LightMan opened 9 years ago

LightMan commented 9 years ago

Hi, I am just trying to test if a notification is posted...

        expect(^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName" object:nil];
        }).to.notify(@"notificationName");

This tests fails with the expected value is nil/null, am I doing something wrong?

I debugged a little and here: EXPMatcherImplementationBegin(postNotification, (id expected)) expected is always nil, so the block is never executed.

Furthermore it would be nice to check this tweet from AshFurrow talking about object:nil when posting the notification.

tema-orange commented 8 years ago

Same for me.

orta commented 8 years ago

you're welcome to take a stab at fixing it?

LightMan commented 8 years ago

Hi orta, nice to see you again \o/

This issue was opened by me on Aug 24, 2015. It was the first time using expecta, so I thought "I must be doing something wrong", that's why I was not sure if the problem was mine, or the library's.

But If as today this issue still exists I could prepare a example project for more experienced users just to verify this problem, and then gladly try to help fixing it.

orta commented 8 years ago

👯

It's worth a quick look, I think Ash was right in that tweet: https://gist.github.com/gfontenot/ would probably do a comparison compare, not a string equals, and dictionary equals ( maybe there's no custom isEquals: on NSNotification

LightMan commented 8 years ago

Nop, I don't see a custom isEquals: on NSNotification, the NSNotifications are filtered to be dispatched to observers with:

/// Returns collection of `NSNotificationReceiver`.
    ///
    /// Will return:
    ///  - elements that property `sender` is `nil` or equals specified parameter `sender`.
    ///  - elements that property `name` is `nil` or equals specified parameter `name`.
    ///
    private func observersMatchingName(_ name:String? = nil, sender: AnyObject? = nil) -> [Iterator.Element] {
        return self.filter { observer in

            let emptyName = observer.name == nil
            let sameName = observer.name == name
            let emptySender = observer.sender == nil
            let sameSender = observer.sender === sender

            return (emptySender || sameSender) && (emptyName || sameName)
        }
    }

I assume that the Objective-C implementation will be similar.

Maybe the problem is in the creation of the matcher. Anyway, I will try to prepare a example project to isolate the issue.