2
我想寫一個數據對象(Realm)使用Alamofire刷新它的屬性的方法。但我無法弄清楚如何進行單元測試。在單元測試中等待Alamofire
import Alamofire
import RealmSwift
import SwiftyJSON
class Thingy: Object {
// some properties
dynamic var property
// refresh instance
func refreshThingy() {
Alamofire.request(.GET, URL)
.responseJSON {
response in
self.property = response["JSON"].string
}
}
}
在我的單元測試中,我想測試Thingy
可以從服務器正確刷新。
import Alamofire
import SwiftyJSON
import XCTest
@testable import MyModule
class Thingy_Tests: XCTestCase {
func testRefreshThingy() {
let testThingy: Thingy = Thingy.init()
testThingy.refreshProject()
XCTAssertEqual(testThingy.property, expected property)
}
我該如何正確設置單元測試?
這正是我所期待的。感謝Rob! –
漂亮的尾隨閉包語法,而不是'thingy.refreshThingy {(string:String?) - >()in' – lazi74
Rob,爲什麼你在'timeout'中放置'5.0'秒而不是'1.0'? –