在带有 swift 的 XCTest 中,您可以在需要它的测试函数中定义模拟对象。像这样
func testFunction(){
class mockClass: AnyObject{
func aFunction(){
}
}
}
我正在尝试使用这些模拟对象来测试另一个函数是否在给定条件下发出正确的通知(在我的情况下,成功通知是用 204 状态代码广播到对象的。
我遇到的问题是我得到一个“无法识别的选择器”运行时错误,即使该deletedSuccess()
函数显然在那里/
这是一些代码转储
func testDelete(){
let expectation = expectationWithDescription("Post was deleted")
class MockReciever : AnyObject {
func deleteSuccess(){
println("delete successfull")
expectation.fulfill()
}
}
let mockReciever = MockReciever()
NSNotificationCenter.defaultCenter().addObserver(mockReciever, selector: "deleteSuccess", name: PostDeletedNotification, object: post)
let response = NSHTTPURLResponse(URL: NSURL(), statusCode: 204, HTTPVersion: nil, headerFields: nil)
let request = NSURLRequest()
post.deleteCompletion(request, response: response, data: nil, error: nil)
waitForExpectationsWithTimeout(30, handler: { (error) -> Void in
if error != nil{
XCTFail("Did not recieve success notification")
} else {
XCTAssertTrue(true, "post deleted successfully")
}
NSNotificationCenter.defaultCenter().removeObserver(mockReciever)
})
}
使用我不知道的模拟对象和选择器是否存在某种问题?