【问题标题】:Unit-testing superclass单元测试超类
【发布时间】:2014-06-04 20:40:00
【问题描述】:

我正在尝试弄清楚如何在 Swift 中重写以下 Objective-C 单元测试:

- (void)testSuperclass {
    Class superclass = [self.animatedView superclass];
    Class expectedSuperclass = [BREAnimatedView class];
    XCTAssertEqualObjects(superclass, expectedSuperclass);
}

【问题讨论】:

    标签: unit-testing swift xctest


    【解决方案1】:

    我相信这应该可行:

    func testSuperclass() {
        XCTAssert(self.animatedView is BREAnimatedView)
    }
    

    这与您的原始条件略有不同,它测试 BREAnimatedView 是否是视图的 direct 超类,而这只是测试视图是否继承自 BREAnimatedView

    【讨论】:

    • 我想测试直接超类,正如你提到的。
    【解决方案2】:

    我想你可以做到

    func testSuperclass() {
        val superclass = self.animatedView.superclass
        val expectedSuperclass = BREAnimatedView.class()
        XCTAssertEqualObjects(superclass, expectedSuperclass)
    }
    

    但我很乐意看看是否有更好的方法。

    【讨论】:

    • 调用 class() 无法编译。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多