2012-10-29 15 views
0

無法弄清楚這裏發生了什麼。 。 。STAssertEqualObjects不能在Xcode中使用OCUnit使用NSString

我有一個簡單的單元測試,旨在測試解析器。測試情況如下:

[parser didStartElement:@"mobileresponse" attributes:[NSDictionary dictionaryWithObject:@"http://www.espn.com/" forKey:@"rooturl"]]; 
[parser didStartElement:@"content" attributes:[NSDictionary dictionaryWithObject:@"/soccer" forKey:@"mobileFriendlyUrl"]]; 
NSString *mobileFriendlyURL = [parser valueForKey:@"mobile_friendly_url"]; 
STAssertEqualObjects(@"http://www.espn.com/soccer", mobileFriendlyURL, @"url path should be appended to root url"); 

現在這個失敗每次,但這裏是輸出

'http://www.espn.com/soccer' should be equal to 'http://www.espn.com/soccer' url path should be appended to root url 

難道我要瘋了或者是那些完全一樣的?有沒有人有任何想法,爲什麼這是拋出一個錯誤?

回答

0

那麼最有可能的原因是,[parser valueForKey:@"mobile_friendly_url"]不會返回NSString

考慮這個例子(錯誤是沒有提出一個觀點):

NSString *mobileFriendlyURL = [NSURL URLWithString:@"http://www.espn.com/soccer"]; 
STAssertEqualObjects(@"http://www.espn.com/soccer", mobileFriendlyURL, @""); 

這將失敗,同樣的消息你的,因爲你比2級的對象是不同的(即使你假設mobileFriendlyURLNSString,並聲明其類型爲NSString)。最後,消息只是打印混淆你的NSURL這當然是相同的字符串,因此的description更加:)

一個快速的方法來測試這個理論是做這樣的事情:

NSString *mobileFriendlyURL = [[NSURL URLWithString:@"http://www.espn.com/soccer"] description]; 
STAssertEqualObjects(@"http://www.espn.com/soccer", mobileFriendlyURL, @""); 

甚至更​​好做一些類似的以查詢類:

NSLog(@"%@", [mobileFriendlyURL class]); 

我希望這是有道理的......

+0

現場感謝! –

0

剛剛到的NSString聲明改爲

[NSString stringWithFormat:@"%@",[parser valueForKey:@"mobile_friendly_url"]]; 
+0

請參閱我的回答。它解釋了爲什麼問題出現在第一個地方... – Alladinian