2016-01-04 74 views
0

貝婁我提到了我的模型對象。XCTest for test model objects

class HVConnection: NSObject { 

    //private var _data: NSMutableDictionary 
    private var _data: NSMutableDictionary 

    // MARK:- Init 

    init(data: NSDictionary) 
    { 
     _data = NSMutableDictionary(dictionary: data) 
    } 

    // MARK:- Properties 
    var last_name: String? { 
     if let lastNameObject = _data.objectForKey("last_name") { 
      return (idObject as! String) 
     } else { 
      return nil 
     } 
    } 
} 

然後我實現了一個測試用例來檢查變量。 貝婁我提到了測試案例。

func testNetworkModelObject() { 

    let connectionObject = ["network": ["first_name": "Dimuth", "last_name": "Lasantha", "business_email": "[email protected]", "currency": "USD", "language": "en-us", "category": "individual"]] 
    let modelObject = HVConnection(data: connectionObject) 

    XCTAssertEqual(modelObject.last_name, "Lasantha") 
} 

波紋管我剛纔提到的錯誤

XCTAssertEqual失敗:( 「無」),不等於( 「可選(」 Lasantha 「)」)

請幫我解決問題

+0

當你創建modelObject,並且你做'modelObject.last_name'它是否返回正確的值?我想也許你傳遞的字典只有一個關鍵/價值與「網絡」 –

回答

0

您的問題是,你不能使用

_data.objectForKey("last_name") 

在您的HVConnection,因爲它嵌套在另一個名爲network的字典鍵中。

所以改用:

// MARK:- Properties 
var last_name: String? { 
    if let lastNameObject = _data.objectForKey("network")?.objectForKey("last_name") { 
     return (lastNameObject as! String) 
    } else { 
     return nil 
    } 
} 

這是展示使用字典:

["network": // your dictionary is a dictionary within a dictionary 
    ["first_name": "Dimuth", 
    "last_name": "Lasantha", 
    "business_email": "[email protected]", 
    "currency": "USD", 
    "language": "en-us", 
    "category": "individual"] 
] 

爲了讓您的last_name,你已經拿到字典鍵network得到真正的字典,一旦你有了這個,你可以檢查你從network密鑰last_name得到的字典。