0

XCTestCase subclass implementation is below:

import XCTest
@testable import AppName

class SomeTestClass: XCTestCase {

    var viewController: AppName.ViewControllerName?
    override func setUp() {
        super.setUp()
        viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("someIdentifier") as? AppName.ViewControllerName // Line 7
        _ = viewController?.view
    }

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

    func testIfCaseInteractionLabelIsNotNil() {
        XCTAssertNotNil(viewController?.someLabel)
    }
}

In the code above the type of view controller object is "AppName.ViewControllerName" if I specify it as just ViewControllerName like below, the test fails because of casting in line 7

    var viewController: ViewControllerName?
    override func setUp() {
        super.setUp()
        viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("someIdentifier") as? ViewControllerName // Line 7
        _ = viewController?.view
    }

I tried making the view controller class public and using @testable annotation in swift2. It doesn't work.

I made "Enable Testability" build setting to YES.

The code works even if I don't use @testable import but in lot of blogs and tutorials, this results in error.

Any help would be appreciated! Thanks in advance

CatVsDogs
  • 342
  • 1
  • 11
  • It sounds like you may have included the storyboard in your test target, in which case remove it. – Mike Taverne Sep 12 '16 at 13:33
  • No I haven't added the storyboard to my test target – CatVsDogs Sep 13 '16 at 04:17
  • let viewController1: AppName.ViewControllerName = ViewControllerName() This line gives a compile time error says "Cannot convert value of ViewControllerName to ViewControllerName" – CatVsDogs Sep 13 '16 at 04:36
  • I found the problem: I have added ViewControllerName class in the build phases -> Compile sources setting. So I had the same class imported twice 1. During @testable import 2. During compile sources – CatVsDogs Sep 13 '16 at 04:55
  • removed it from compile sources and now its working. Thanks Mike for the comment – CatVsDogs Sep 13 '16 at 04:56

0 Answers0