强制一个类职位的特定NSNotification?
-
21-08-2019 - |
题
有没有什么办法,以确保一类的帖子特定NSNotification?
(I有一组类,我想在编译时执行(如果可能),类讯息所需NSNotification)。
可选地,如果这是不可能的,有没有任何解决办法:
解决方案
这是从根本上不可能在编译时在运行时会发生什么预测。你可以得到最接近的是静态分析,但即使无法预知发生的任何事情你自己的代码之外,如内部基金会。
可以,但是,这样做与单元测试,因为测试运行实际上被测运行的代码。
您需要创建一个测试包的目标,如果你还没有。你的目标将使用SenTestingKit运行测试,您创建。 (在iPhone上,你还需要谷歌工具箱,呃,Mac电脑。他们有的使用GTM用于iPhone一个方便的教程测试。)
您将创建一个子类SenTestCase来测试你是否真正的对象帖子的通知。它会是这个样子:
@interface FrobnitzerNotificationsTest: SenTestCase
{
BOOL frobnitzerDidCalibrate;
}
- (void) frobnitzerDidCalibrate:(NSNotification *)notification;
@end
@implementation FrobnitzerNotificationsTest
- (void) testFrobnitzerCalibratePostsNotification {
Frobnitzer *frobnitzer = …;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(frobnitzerDidCalibrate:)
name:FrobnitzerDidCalibrate
object:frobnitzer];
frobnitzerDidCalibrate = NO;
//This should post a notification named FrobnitzerDidCalibrate with the receiver as the object.
[frobnitzer calibrate];
//If it did, our notification handler set frobnitzerDidCalibrate to YES (see below).
[nc removeObserver:self
name:FrobnitzerDidCalibrate
object:frobnitzer];
STAssertTrue(frobnitzerDidCalibrate, @"Frobnitzer did not post a notification when we told it to calibrate");
}
- (void) frobnitzerDidCalibrate:(NSNotification *)notification {
frobnitzerDidCalibrate = YES;
}
@end
您将需要一个实例变量,并为您希望测试每一个通知,通知处理程序方法,并且一个测试方法要测试的通知每一个方法。
此外,如果使用GTM,则必须替换GTMSenTestCase上面SenTestCase。
不隶属于 StackOverflow