質問

クラスのポスト特定NSNotificationていることを確実にする方法はありますか?

は、((可能ならば)私はクラスのセットを持っている、と私はコンパイル時に施行したいと思い、そのクラスのポストに必要なNSNotification)。

それが不可能な場合は、

あるいは、任意の回避策はありますか?

役に立ちましたか?

解決

これは、実行時に何が起こるか、コンパイル時に予測することは根本的に不可能です。あなたが得ることができる最も近い静的解析ですが、でもそれは、財団の内部など、独自のコードの外側で起こる何かを予測することはできません。

テストランナーは、実際にテスト中のコードが実行されますので、

あなたが、しかし、ユニットテストでこれを行うことができます。

あなたがまだの場合は、テストバンドルターゲットを作成する必要があります。あなたの目標は、あなたが作成した、あなたのテストを実行するためにSenTestingKitを使用します。 (iPhone上で、あなたがGoogleのツールボックスも必要になります、ええと、マック。彼らは持っている<のhref =「http://code.google.com/p/google-toolbox-for-mac/wiki/iPhoneUnitTesting」のrel = "nofollowをnoreferrer"> iPhone用のGTMの使用に関する便利なチュートリアルでは、してテストします。)

あなたはあなたの本当の目的ポストかどうかの通知をテストする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

あなたは、通知のためにテストしたいすべてのメソッドのための1つのインスタンス変数とあなたがテストしたいすべての通知のための1つの通知ハンドラ・メソッド、および1つの試験方法が必要になります。

また、GTMを使用する場合は、上記のSenTestCaseためGTMSenTestCaseを置き換える必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top