如何断言结构符合协议?

How can I assert a struct conforms to a protocol?

我有一个断言 class 符合协议的测试用例。

        let sut = SomeClass()    
        ..........
        func test_some_class_conform_to_protocol() {
                XCTAssertTrue((sut as Any) is OverlayManagerType)

        }

我正在尝试使用符合协议的 struct 实施相同的测试,但是测试总是失败。

有可能实现吗?

编辑

我已经添加了我的结构。我正在遵循 TDD 方法,因此目前还没有实现。

protocol CountManagerType {

}

struct CountManager: CountManagerType {

}

我的测试是

    func test_count_manager_conform_to_protocol() {
            XCTAssertTrue((sut as Any) is CountManagerType)

    }

在下面的示例中,您的代码对我来说工作正常

protocol CountManagerType {
}
struct CountManager1: CountManagerType {
}
struct CountManager2 {
}

let c1 = CountManager1()
print(((c1 as Any) is CountManagerType)) // true
let c2 = CountManager2()
print(((c2 as Any) is CountManagerType)) // false

我运行这个代码在操场上,似乎没问题:


    import UIKit
    import XCTest

    protocol CountManagerType {

    }

    struct CountManager: CountManagerType {

    }

    struct CountManager1 {

    }

    class CountManagerTests: XCTestCase {
      override func setUp() {
          super.setUp()
      }

      func test_countmanager_conform_to_protocol() {
        XCTAssertTrue((CountManager() as Any) is CountManagerType)
      }

      func test_countmanager1_conform_to_protocol() {
        XCTAssertTrue((CountManager1() as Any) is CountManagerType)
      }
    }

    CountManagerTests.defaultTestSuite.run()