LoginSignup
1

More than 5 years have passed since last update.

XCTestでUIStoryboard.instantiateViewControllerWithIdentifierがSIG_ABRTする

Last updated at Posted at 2016-07-27

解決方法

  • 今わかってる解決方法としては ProductModuleName をUnitTestと同じにしないこと
  • あとObjC側でUIStoryboardつかって直接インスタンス化すれば動くけど、いまいち…

エラー

Could not cast value of type 'Testable.ViewController' (0x100e5aee0) to 'Testable.ViewController' (0x10cb67510).

発生条件

  • SwiftをObjCのXCTestでテストする
    • SwiftをSwiftのXCTestでも起きる
  • ProductNameとUnitTestのProductNameが同じ
    • この例で言うとどちらも Testable になっている状態

開発環境

  • Xcode: 7.3.1
  • DeployTarget: iOS9
  • iPhoneSimulator: iOS9.3

再現コード

  • 新しいプロジェクトを作成
    • include Unit Test にチェック
    • Testableというプロジェクト名にする
  • UnitTestのProductModuleNameを $(PRODUCT_NAME) から Testable に書き換える
  • あとPackagingのDefinesModuleをYESにする(ObjCにSwiftを読み込むため)

Testable

  • ProductName: Testable

import UIKit

class ViewController: UIViewController {
    class func instanceFromStoryboard() -> ViewController {
        return UIStoryboard(name: "Main", bundle: NSBundle(forClass: self)).instantiateViewControllerWithIdentifier("ViewController") as! ViewController
    }
}

上記 bundle の引数を nil にすると
Could not cast value of type 'Testable.ViewController' (0x109919ed0) to 'TestableTests.ViewController' (0x115625328).
というキャストエラーがでるので注意

TestableTest ( UnitTest )

  • ProductName: Testable
#import <XCTest/XCTest.h>
#import "Testable-Swift.h"

@interface ObjCTest : XCTestCase

@end

@implementation ObjCTest

- (void)testStoryboard
{
    [ViewController instanceFromStoryboard]; //SIGABRT
}

@end

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
1