LoginSignup
0
1

More than 3 years have passed since last update.

【エラー対処】【ゆめみiOS研修】Swift Unit Test -> Could not cast value of type 'xxx' to 'xxxTests'

Posted at

株式会社ゆめみ iOS研修
システムエンジニアとして働いているものコードを書く機会が少ないため、プライベートでコードを書く習慣を身につけたいと思い見つけた新人育成用カリキュラム。

Session8でのテストコード作成時に以下のエラーで躓いたため備忘録として記載します。

エラーログ

Could not cast value of type 'yumemi.ViewController' (0x108f463f8) to 'yumemiTests.ViewController'

テストコードでUIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()よりViewControllerを生成しようとしたが、実行時にエラーとなる。
ログの内容的にTargetの呼び出し先が関係していそう。

バージョン

ツール バージョン
Xcode 12.4
Swift 5

テストコード

ViewControllerTests.swift
import XCTest
@testable import yumemi

class ViewControllerTests: XCTestCase {

    var viewController: ViewController!

    override func setUpWithError() throws {
        viewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        _ = viewController.view
    }

    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func testExample() throws {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }

    func testPerformanceExample() throws {
        // This is an example of a performance test case.
        self.measure {
            // Put the code you want to measure the time of here.
        }
    }
}

解決方法

テスト対象のViewControllerを参照するとTarget MembershipにTest Target(今回はyumemiTests)が含まれていたため、チェックを外したらエラーは解消され、テストコードからViewControllerを参照できるようになった。

その他

解決はしたものの理由は分かっていないため、ご存知の方がいればご教授頂けますと嬉しいです。
まだ綺麗にコードを書けていませんが奮闘中です。
yumemi個人演習用リポジトリ

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1