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
}
}
}
In my unit test, I I want to test whether Thingy can refresh the server normally.
import Alamofire
import SwiftyJSON
import XCTest
@testable import MyModule
< br />class Thingy_Tests: XCTestCase {
func testRefreshThingy() {
let testThingy: Thingy = Thingy.init()
testThingy.refreshProject()
XCTAssertEqual( testThingy.property, expected property)
}
How to properly set up unit tests for this?
p>
func testExample() {
let e = expectation(description: "Alamofire")
Alamofire.request(urlString)
.response {response in
XCTAssertNil(response.error, "Whoops, error \(response.error!.localizedDescription)")
XCTAssertNotNil(response, "No response")
XCTAssertEqual(response.response? .statusCode ?? 0, 200, "Status code not 200")
e.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil )
}
In your case, if you want to test an asynchronous method, you must provide a completion handler for refreshThingy:
class Thingy {
var property: String!
func refreshThingy(completionHandler: ((String?) -> Void)?) {
Alamofire.request(someURL)
.responseJSON {response in
if let json = response.result.val ue as? [String: String] {
completionHandler?(json["JSON"])
} else {
completionHandler?(nil)
}
}< br /> }
}
Then you can test Thingy:
func testThingy() {
let e = expectation( description: "Thingy")
let thingy = Thingy()
thingy.refreshThingy {string in
XCTAssertNotNil(string, "Expected non-nil string")
e.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil)
}
Frankly, in any case, this use The completion handler mode may be the mode you want to use in refreshThingy, but if you may not want to provide a completion handler, I will make it optional.
I am trying to write a method where the data object (Realm) uses Alamofire to refresh its properties. But I can’t figure out how to unit test it.
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
}
}
}
In my unit test, I want to test whether Thingy can refresh the server normally.
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)
}
How to properly set up unit tests for this?
Use XCTestExpectation to wait for an asynchronous process, for example:
func testExample() {
let e = expectation(description: "Alamofire")
Alamofire.request(urlString)
.response {response in
XCTAssertNil(response.error, "Whoops, error \(response.error!.localizedDescription)")
XCTAssertNotNil(response, "No response")
XCTAssertEqual(response.response?.statusCode ?? 0, 200, "Status code not 200")
e.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil)
}
In your case, if you want to test an asynchronous method, you must provide a completion handler for refreshThingy:
class Thingy {
var property : String!
func refreshThingy(completionHandler: ((String?) -> Void)?) {
Alamofire.request(someURL)
.responseJSON {response in
if let json = response.result.value as? [String: String] {
compl etionHandler?(json["JSON"])
} else {
completionHandler?(nil)
}
}
}
}
Then you can test Thingy:
func testThingy() {
let e = expectation(description: "Thingy")
let thingy = Thingy()
thingy.refreshThingy {string in
XCTAssertNotNil(string, "Expected non-nil string")
e.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil)
}
Frankly, anyway, this mode of using completion handlers may be what you want in refreshThingy The mode used, but if you may not want to provide a completion handler, I will make it optional.