我想測試我在數據庫中插入的值是否發回給我的類的委託。OCMock - 如何期待nsarray的內容
我試圖模擬委託,並期望我用來填充數據庫的數組。它失敗,因爲2 NSArray具有相同的內容,但是是不同的對象。
我試過在我的模型沒有成功實現isequal和哈希方法。
以下是錯誤日誌:
'OCMockObject[NewsListViewController]: unexpected method invoked: service:<RSSService-0x4c25b90-324400011.636966: 0x4c25b90> didFinishParsingRSSWithItems:(
description102362,
description102362,
description102362,
description102362,
description102362
)
expected: service:<OCMAnyConstraint: 0x4c10f30> didFinishParsingRSSWithItems:(
description102362,
description102362,
description102362,
description102362,
description102362
)'
我怎樣才能做到這一點?
這裏是我的測試:
- (void) testServiceShouldNotLoadArticlesFromRSSFeedIfArticlesInDatabase {
NSArray *fakeArticles = [TestUtils createArticles:5];
[[DatabaseManager sharedManager] saveArticles:fakeArticles];
RSSService *mockService = [OCMockObject partialMockForObject:service];
id mockDelegate = [OCMockObject mockForClass:NewsListViewController.class];
[[mockDelegate expect] service:[OCMArg any] didFinishParsingRSSWithItems:fakeArticles];
mockService.delegate = mockDelegate;
[mockService loadAllArticles];
[mockService verify];
[mockDelegate verify];
}
這裏是方法我測試:
- (void) loadAllArticles {
NSArray *articles = [self articlesFromDatabase];
[self.delegate service:self didFinishParsingRSSWithItems:articles];
}
感謝您的幫助, 文森特
它應該做什麼?我在構建時得到了一個警告:「警告:語義問題:不兼容的指針類型將'id *'發送給'NSArray *'類型的參數並導致崩潰:reason:' - [__ NSArrayM pointerValue]:發送到實例的無法識別的選擇器0x6242960' – vdaubry 2011-04-14 10:00:40
我更新了我的答案 - 試試這個。第一篇文章絕對不正確。 – AdamH 2011-04-14 12:48:16