如何将 XCTAssertNil 与可选结构一起使用? [英] How can one use XCTAssertNil with optional structs?

查看:25
本文介绍了如何将 XCTAssertNil 与可选结构一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2016 年 3 月 23 日更新我刚刚在下面测试了我的原始示例代码,它在 XCode 7.3 中编译得很好.看起来 XCTAssertNil 已更新为采用 () throws -> 类型的表达式.有吗?因此这个问题和答案可能不再需要(除了一些旧版本的编译器.)

Update 3/23/2016 I just tested my original sample code below and it all compiles fine in XCode 7.3. Looks like XCTAssertNil was updated along the way to take an expression of type () throws -> Any? Therefore this question and answer may be no longer needed (except for a while with older versions of the compiler.)

我正在使用 XCTest 在 XCode 中编写我的第一个单元测试.我不确定如何利用 XCTAssertNil,因为它似乎只在使用某些类型时才能编译.看起来它可以与由类和内置原语组成的选项一起工作,但不能与结构一起工作.如何使用这种方法?

I'm writing my first unit tests in XCode with XCTest. I'm unsure how one can take advantage of XCTAssertNil as it seems to only compile when using certain types. It appears it will work with optionals made from classes and built-in primitives, but not structs. How would one go about using this method?

对于结构,编译器给出以下错误(假设SimpleStruct"是您的类型名称):

For structs the compiler gives the following error (assuming 'SimpleStruct' is the name of your type):

'SimpleStruct' is not identical to 'AnyObject'

这是一个简单的测试类,用于说明一些可以编译的类型,而另一些则不能.

Here's a simple test class to illustrate some of the types that compile okay and other's that don't.

import Cocoa
import XCTest

struct SimpleStruct {
}

class SimpleClass {
}

class Tests: XCTestCase {

    func testl() {
        var simpleStruct:SimpleStruct? = nil;
        var simpleClass:SimpleClass? = nil;
        var i:Int? = nil;
        var s:String? = nil;
        var tuple:(Int,String)? = nil;

        XCTAssertNil(simpleStruct); // compile error
        XCTAssertNil(simpleClass); // OK
        XCTAssertNil(i); // OK
        XCTAssertNil(s); // OK
        XCTAssertNil(tuple); // compile error
    }

}

推荐答案

Update 3/23/2016 已针对 XCode 7.3 更新(但是,如果您看到我对问题的编辑,则会出现此解决方法不再需要)

Update 3/23/2016 Updated for XCode 7.3 (however if you see my edit to the question, it would appear this workaround is no longer needed)

这是一个解决方法.我创建了自己的通用函数:

Here is a workaround. I created my own generic function:

func AssertNil<T>(@autoclosure expression: () -> T?, message: String = "",
               file: StaticString = #file, line: UInt = #line) {

    if (expression() != nil) {
        XCTFail(message, file:file, line:line)
    }
}

这似乎没有必要.这是否只是 XCTest 最初针对 Objective-C 而尚未针对 Swift 进行足够更新/桥接的结果?

Doesn't seem like this should be necessary. Is this just a result of XCTest originally targeting Objective-C and not being updated/bridged enough for Swift yet?

我已经做了足够的研究,发现 AnyObject 可用于表示任何类,但不能表示结构.但是,这并不能解释为什么我的原始帖子中的代码针对 Int 类型和 String 类型进行编译.(我在其他地方读到 Xcode 可能会自动将这些转换为 NSNumber 和 NSString,这可能会解释原因.请参阅 http://www.scottlogic.com/blog/2014/09/24/swift-anyobject.htmlhttp://www.drewag.me/posts/swift-s-weird-handling-of-basic-value-types-and-anyobject.我将尝试删除导入 Foundation 的 Cocoa 的导入,看看会发生什么)

I've done enough research to see that AnyObject can be used to represent any class but not structs. However, that then doesn't explain why the code in my original post compiles for Int types and String types. (I read somewhere else that Xcode may auto convert these to NSNumber and NSString for you which might explain why. See http://www.scottlogic.com/blog/2014/09/24/swift-anyobject.html and http://www.drewag.me/posts/swift-s-weird-handling-of-basic-value-types-and-anyobject. I'll try removing my import of Cocoa which imports Foundation to see what happens)

Edit2:XCTest 还导入 Foundation,因此我无法测试我想要的内容.我可以创建自己的方法并对此进行测试.现在,我假设自动转换是允许 int 和 string 选项编译的原因.似乎 XCTest 还没有准备好迎接 Swift 的黄金时间.

XCTest also imports Foundation so I can't test what I wanted to. I could create my own methods and test this. For now, I assume that the auto-conversions are what are allowing the int and string optionals to compile. Seems like XCTest isn't quite ready for prime time with Swift.

更新 8/13/2015:编辑功能以兼容 XCode 7 beta

Update 8/13/2015: Edited the function to be compatible with XCode 7 beta

这篇关于如何将 XCTAssertNil 与可选结构一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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