use*_*621 14 unit-testing nsnotificationcenter ios swift ios8
我在swift工作,我想刷新一个页面,所以我使用通知发送它,我在一个ViewController中发布通知并在另一个中添加观察者,它工作正常.我想要做的是在swift中添加单元测试.我查了很多网站但是没能做到.我是新手,不知道从哪里开始.
基本上工作是,当我点击按钮通知发布时,当加载下一个视图控制器时,添加通知观察器.
我该怎么做单元测试
提前致谢
编辑:代码
NSNotificationCenter.defaultCenter().postNotificationName("notificationName", object: nil)
Run Code Online (Sandbox Code Playgroud)
并添加观察者
NSNotificationCenter.defaultCenter().addObserver(self, selector: "vvv:",name:"notificationName", object: nil)
Run Code Online (Sandbox Code Playgroud)
zpa*_*ack 16
XCTest有一个专门用于测试通知的类:XCTNSNotificationExpectation.您可以创建其中一个期望,并在收到通知时完成.你会像以下一样使用它:
// MyClass.swift
extension Notification.Name {
static var MyNotification = Notification.Name("com.MyCompany.MyApp.MyNotification")
}
class MyClass {
func sendNotification() {
NotificationCenter.default.post(name: .MyNotification,
object: self,
userInfo: nil)
}
}
// MyClassTests.swift
class MyClassTests: XCTestCase {
let classUnderTest = MyClass()
func testNotification() {
let notificationExpectation = expectation(forNotification: .MyNotification,
object: classUnderTest,
handler: nil)
classUnderTest.sendNotification()
waitForExpectations(timeout: 5, handler: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
XCTestCase这expectation(forNotification:object:handler:)是一种创建实例的便捷方法XCTNSNotificationExpectation,但为了更多控制,您可以实例化一个并自己配置它.查看文档.
Bry*_*hen 12
一般的解决方案是:使用依赖注入(DI)使您的组件可单元测试.您可以选择使用DI框架(我不知道Swift是否存在任何良好的框架)或使用本机方法(即传递对象)
解决问题的一种可能方法是将其包装NSNotificationCenter为可模拟/可注入.
这只是一个基本的想法,你可以如何解耦依赖.请不要只复制并粘贴下面的代码,并希望它在不理解的情况下工作.
import Foundation
protocol NotificationCenter {
func postNotificationName(name: String, object: AnyObject?)
// you can make it take the arguments as NSNotificationCenter.addObserver
func addObserver(callback: AnyObject? -> Void)
}
class MyNotificationCenter : NotificationCenter {
var _notificationCenter: NSNotificationCenter
init(_ center: NSNotificationCenter) {
_notificationCenter = center
}
func postNotificationName(name: String, object: AnyObject?) {
// call NSNotificationCenter.postNotificationName
}
func addObserver(callback: AnyObject? -> Void) {
// call NSNotificationCenter.addObserver
}
}
class MockNotificationCenter : NotificationCenter {
var postedNotifications: [(String, AnyObject?)] = []
var observers: [AnyObject? -> Void] = []
func postNotificationName(name: String, object: AnyObject?) {
postedNotifications.append((name, object))
}
func addObserver(callback: AnyObject? -> Void) {
observers.append(callback)
}
}
class MyView {
var notificationCenter: NotificationCenter
init(notificationCenter: NotificationCenter) {
self.notificationCenter = notificationCenter
}
func handleAction() {
self.notificationCenter.postNotificationName("name", object: nil)
}
}
class MyController {
var notificationCenter: NotificationCenter
init(notificationCenter: NotificationCenter) {
self.notificationCenter = notificationCenter
}
func viewDidLoad() {
self.notificationCenter.addObserver {
println($0)
}
}
}
Run Code Online (Sandbox Code Playgroud)
// production code
// in AppDeletate.applicationDidFinishLaunching
let notificationCenter = MyNotificationCenter(NSNotificationCenter.defaultCenter())
// pass it to your root view controller
let rootViewController = RootViewController(notificationCenter: notificationCenter)
// or
rootViewController.notificationCenter = notificationCenter
// in controller viewDidLoad
self.myView.notificationCenter = self.notificationCenter
// when you need to create controller
// pass notificationCenter to it
let controller = MyController(notificationCenter: notificationCenter)
// in unit test
func testMyView() {
let notificationCenter = MockNotificationCenter()
let myView = MyView(notificationCenter: notificationCenter)
// do something with myView, assert correct notification is posted
// by checking notificationCenter.postedNotifications
}
func testMyController() {
let notificationCenter = MockNotificationCenter()
let myController = MyController(notificationCenter: notificationCenter)
// assert notificationCenter.observers is not empty
// call it and assert correct action is performed
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7458 次 |
| 最近记录: |