XCTest并使用模拟对象作为通知接收者(XCTest and using mock objects as notification receivers)

编程入门 行业动态 更新时间:2024-10-26 03:37:34
XCTest并使用模拟对象作为通知接收者(XCTest and using mock objects as notification receivers)

在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.

更多推荐

本文发布于:2023-08-04 11:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收者   对象   通知   XCTest   receivers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!