2013-09-28 51 views
3

我試圖寫一個XCTest(iOS7,XCode5)與OCMock一起CLLocationManager模擬類方法。無法使用OCMock與iOS7

我有它實現了CLLocationManagerDelegate協議的類,並且有一個屬性是一個CLLocationManager的一個實例。 (我將CLLocationManager的實例提供給我的initialiser方法,以便我可以在運行時或測試中注入它)。

當測試委託類,我創建了一個模擬CLLocationManager。

在測試中,我要實現的是這樣的:

[[[[mockLocationManager stub] classMethod] andReturnValue:kCLAuthorizationStatusDenied] authorizationStatus]; 
result = [delegateUnderTest doMethod]; 
//Do asserts on result etc etc 

的問題是,Xcode是抱怨我的代碼。

test.m:79:68: Implicit conversion of 'int' to 'NSValue *' is disallowed with ARC 
test.m:79:68: Incompatible integer to pointer conversion sending 'int' to parameter of type 'NSValue *' 

kCLAuthorizationStatusDenied是int我明白(如在一個typedef定義)。 所以,我不能用

[[[[mockLocationManager stub] classMethod] andReturn:kCLAuthorizationStatusDenied] authorizationStatus]; 

其期望的對象(「andReturn」是一個「身份證」)。

任何想法?

回答

2

您需要框在NSValue實例中的價值,而不是通過原始值本身。例如:

[[[mockLocationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus]; 

上面利用爲NSNumber S中的目標C字面語法。另外,我省略classMethod呼叫以上CLLocationManager沒有一個實例方法authorizationStatus

此更多的支持可在所述OCMock website找到:

如果該方法返回原始類型然後andReturnValue:必須以一個參數值來使用。直接傳遞基本類型是不可能的。

這也是什麼編譯器錯誤是告訴你 - 你是傳遞一個int而不是NSValue實例。

+0

謝謝。完成這個訣竅。我曾嘗試過@kCLAuthorizationStatusDenied(沒有括號),這似乎是正確的。那些額外的括號@(kCLAuthorizationStatusDenied)將我排序。 –