在XCTest with swift中,您可以在您需要的测试函数中定义模拟对象。就像这样
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) }) }使用这种我不知道的模拟对象和选择器有什么问题吗?
In XCTest with swift you can define mock objects in the test function you need it in. Like so
func testFunction(){ class mockClass: AnyObject{ func aFunction(){ } } }I'm trying to use these mock objects to test that another function sends out the correct notification given a certain condition (in my case that the success notification is broadcast with a 204 status code to an object.
The problem I am having is that i get an "Unrecognised selector" runtime error even though the deletedSuccess() function is clearly there/
Heres some code dump
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) }) }Is there some sort of problem with using mock objects and selectors like this that I don't know about?
最满意答案
您不必实现模拟对象来测试通知。 有一个-[XCTestCase expectationForNotification:object:handler:]方法。
下面是关于如何从Swift类中接收NSNotificationCenter的通知的答案 ,这些类不从NSObject继承。 从技术上讲,这是一个重复的问题。
You don't have to implement mock objects to test notifications. There is a -[XCTestCase expectationForNotification:object:handler:] method.
And here's an answer on how to receive notifications from NSNotificationCenter in Swift classes that do not inherit from NSObject. Technically it's a duplicate question.
更多推荐
发布评论