ios – 在XCTestCase子类中使用泛型有效吗?

我有一个XCTestCase子类,看起来像这样.为了简洁起见,我已经删除了setup()和tearDown方法
class ViewControllerTests <T : UIViewController>: XCTestCase {
    var viewController : T!

    final func loadControllerWithNibName(string:String) {
        viewController  = T(nibName: string,bundle: NSBundle(forClass: ViewControllerTests.self)) 
        if #available(iOS 9.0,*) {
            viewController.loadViewIfNeeded()
        } else {
            viewController.view.alpha = 1
        }
    }
}

它的子类看起来像这样:

class WelcomeViewControllerTests : ViewControllerTests<WelcomeViewController> {
    override func setUp() {
        super.setUp()
        self.loadControllerWithNibName("welcomeViewController")
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    func testName() {
       let value =  self.viewController.firstNameTextField.text
        if value == "" {
            XCTFail()
        }
    }
}

在理论上,这应该按预期工作 – 编译器不会抱怨任何事情.但是只是当我运行测试用例时,setup()方法甚至没有被调用.但是,它表明当testName()方法应该失败时,测试已经过去了.

使用泛型是一个问题吗?我可以很容易地想到很多非通用的方法,但是我很想写这样的测试用例.这是XCTest在Objective-C和Swift之间的互操作性?

解决方法

XCTestCase使用Objective-C运行时加载测试类并找到测试方法等.

Swift通用类与Objective-C不兼容.见https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-ID53

When you create a Swift class that descends from an Objective-C class,the class and its members—properties,methods,subscripts,and initializers—that are compatible with Objective-C are automatically available from Objective-C. This excludes Swift-only features,such as those listed here:

  • Generics

Ergo您的通用XCTestCase子类不能被XCTest使用.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...