2003/06/01
前回のつづきです。
- (id) add: (id)another
{
value += [another intValue];
return self;
}
|
- (id) add: (id)another
{
value = 2;
return self;
}
|
- (void) testAdd
{
SmallInteger* one = [SmallInteger initWithInt:1];
SmallInteger* two = [one add:[SmallInteger initWithInt:1]];
[self assert:two equals:[SmallInteger initWithInt:2]];
[two add:[SmallInteger initWithInt:0]];
[self assert:two equals:[SmallInteger initWithInt:3]];
}
|
..F FAILURES!!! Tests run:2, Failures:1, Errors: 0 There was 1 failure: 1) testAdd(SmallTest)"expected <SmallInteger: 0x7f770> but was <SmallInteger: 0x7f730>" |
- (NSString *) description
{
return [NSString stringWithFormat:@"SmallInteger(%d)", value];
}
|
..F FAILURES!!! Tests run:2, Failures:1, Errors: 0 There was 1 failure: 1) testAdd(SmallTest)"expected SmallInteger(3) but was SmallInteger(2)" |
- (void) testAdd
{
SmallInteger* one = [SmallInteger initWithInt:1];
SmallInteger* two = [one add:[SmallInteger initWithInt:1]];
[self assert:two equals:[SmallInteger initWithInt:2]];
[two add:[SmallInteger initWithInt:0]];
[self assert:two equals:[SmallInteger initWithInt:2]];
}
|
メソッドdescriptionを書いたら、オブジェクトの内容を表示できるようになりました。しかし、表示できたからと言って、正しく表示されているとは限りません。ここで注意しなければいけないことは「2になるはずなのに、3だよ」ではないということです。説明上、例を簡単にしているから、この例のようにテストプログラムを間違うわけないと思っているかもしれませんが、あなたが書いたテストプログラムだって間違うかもしれないのです。そのとき、運が悪いと、テストされたプログラムが正しいのに、テストは失敗します。
正しく表示できるのは、正しく使ったときだけです。メソッドassert:equals:の正しい使い方です。
[self assert:<テストされる値> equals:<テスト結果として正しい値>]; |