swift 中的嵌套函数选择器用于测试 [英] Nested function selector in swift for testing

查看:25
本文介绍了swift 中的嵌套函数选择器用于测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况,也许有更简单的方法来做到这一点:

Here is my situation and perhaps there is an easier way to do this:

我正在测试一些使用通知的东西,我不想将我的期望定义为类级别的可选变量,所以我想知道是否可以将它们设为函数的局部变量,以便我的通知处理程序可以访问它们.

I'm testing some stuff that uses notifications and I didn't want to have to define my expectations as class level optional variables so I was wondering if I can make them local variables to a function in such a way that my notification handler can access them.

我的尝试是将通知处理程序函数作为我的顶级测试函数中的嵌套函数 - 但我遇到了选择器命名问题,因为我不确定我需要告诉通知处理程序调用什么

My attempt was to make notification handler functions as nested functions inside my top level test function - but i've run into selector naming issues as I'm not sure what I need to tell the notification handler to call

class FilePlayerTests: XCTestCase {

func testFilePlayback() {


    let f1URL : NSURL = NSBundle(forClass: FilePlayerTests.self).URLForResource("test1", withExtension: "csv")!
    let f2URL : NSURL = NSBundle(forClass: FilePlayerTests.self).URLForResource("test2", withExtension: "csv")!
    let f3URL : NSURL = NSBundle(forClass: FilePlayerTests.self).URLForResource("test3", withExtension: "csv")!

    let f1 = dm.createFilePlayerFromURL(f1URL)
    let f2 = dm.createFilePlayerFromURL(f2URL)
    let f3 = dm.createFilePlayerFromURL(f3URL)


    let e1 = expectationWithDescription("xplane1")
    let e2 = expectationWithDescription("xplane2")
    let e3 = expectationWithDescription("xplane3")



    f1?.startPlayback()


    //Define LocationMessage Observer
    nc.addObserver(self, selector: "newHandler:",
        name: dmNotification.LocationData.rawValue,
        object: nil)


    ///Prints out a new Location Message
    func newHandler(notif: NSNotification) {
        let msg = notif.asLocationMessage!
        println(msg)

        e1.fulfill()
    }

}
}

所以我的代码崩溃了,因为它找不到选择器.

So my code is crashing because it can't find the selector.

1) 这有效吗?

2) 我如何正确命名选择器以便找到它?

2) How would i name the selector correctly so it could be found?

推荐答案

问题在于你是这样说的:

The problem is that you are saying this:

nc.addObserver(self, selector: "newHandler:" ...

但是 self,FilePlayerTests 类,没有名为 newHandler: 的选择器 - 因为您仅将该函数定义为 testFilePlayback 中的局部函数代码>功能.它只存在于本地——仅在 testFilePlayback 函数内运行的代码眼中——并且只是非常临时的,即当 testFilePlayback 正在运行时.

But self, the FilePlayerTests class, has no selector called newHandler: - because you have defined that function only as a local function inside the testFilePlayback function. It exists only locally - only in the eyes of code that runs after it inside the testFilePlayback function - and only very temporarily, i.e. while testFilePlayback is running.

您必须在 FilePlayerTests 类的顶层定义 newHandler: 以便它是通知中心可以实际调用的方法.

You must define newHandler: at the top level of the FilePlayerTests class so that it is a method that the notification center can actually call.

当然,这可能(即将)意味着您还必须将方法中的其他一些内容提升到顶级水平.

That may (i.e. will) mean that you will have to promote some other stuff in your method to top level as well, of course.

这篇关于swift 中的嵌套函数选择器用于测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆