OCMock有special way个模拟/间谍NSNotificationCenter。您可以使用它来查看给定的通知是否像这样发布:
id observerMock = OCMObserverMock();
[NSNotificationCenter.defaultCenter addMockObserver:observerMock name:@"MyNotificationEventNameHere" object:nil];
OCMExpect([observerMock notificationWithName:@"MyNotificationEventNameHere" object:OCMOCK_ANY]);
// Do something to cause the notification to fire here
OCMVerifyAll(observerMock);
或者,您也可以使用部分模拟来完成同样的事情:
id notificationCenterSpy = OCMPartialMock(NSNotificationCenter.defaultCenter);
// Do something to cause the notification to fire here
OCMVerify([notificationCenterSpy postNotificationName:@"MyNotificationEventNameHere" object:OCMOCK_ANY]);
问题是为什么我更喜欢OCMock的Observer Mock模式与Partial Mock模式进行通知中心验证?