Use of Undeclared Type 'Viewcontroller' When Unit Testing My Own Viewcontroller in Swift

Use of undeclared type 'ViewController' when unit testing my own ViewController in Swift?


Swift 1

You should add ViewController.swift file's target membership also as your test target also if you are not using framework. Select class file add to target as shown in image:

Sample Image

OR

If you are ViewController is within a framework : ViewController class is in different target and You are not declaring class with public access level. By default Classes are internal (Accessible within a target). Declare it as public and also make methods or properties as public if you want to access it i.e

public class ViewController: UIViewController {

public var content: String!

override public func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Swift 2 Update

In your test target, just import the module you want to test using the @testable keyword:

@testable import moduleToTest

You can now access public and internal symbols in your test target.

swift 2 Xcode 7 unit testing

Can't cast custom UIViewController from UIStoryboard in XCTest

I came up with a better solution than making everything public. You actually just need to use a storyboard that comes from the test bundle instead of using nil (forcing it to come from the main target's bundle).

var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
vc = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as LoginViewController
vc.loadView()

Swift 'use of undeclared type'

You are mixing class names and instance variable names. Class names should be upper case: MasterTableViewController, AddViewController, DeleteViewController

At first, try to distinguish between the class name and the instance variable name by choosing a different name, i.e.:

var dvc:deleteViewController = segue!.destinationViewController as deleteViewController
dvc.todoData = ...

And see if it works

Use of undeclared type in Swift, even though type is internal, and exists in same module

Phew, finally diagnosed this. Somehow, the offending Swift file EditTaskPopoverController.swift was in two different build phases.

It was in Compile Sources properly, with all the other Swift files, but it was also, for some very strange reason, in the Copy Bundle Resources phase as well, along with all my XIB and image resources.

I have no idea how it got there, but removing it from the extra build phase resolved the issue.

Unit test case view controller crash swift

After lots of trial n error we figured out the following code works for us. The issue was we were passing 'nil' value in bundle parameter. When this was replaced with the following line it started to work.
bundle: NSBundle(forClass: self.dynamicType))

The working code:

let storyboard = UIStoryboard(name: "Clients", bundle: NSBundle(forClass: self.dynamicType))

var vc = storyboard.instantiateViewControllerWithIdentifier("ClientsVCTable") as ClientsTableViewController

vc.loadView()

XCTAssertNotNil(vc.clientSearchBar,"Not Nil")

Also for the IBActions:

let storyboard = UIStoryboard(name: "Login", bundle: NSBundle(forClass: self.dynamicType))

var vc = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as LoginViewController

vc.loadView()

let actions : NSArray = vc.signInButton.actionsForTarget(vc, forControlEvent: UIControlEvents.TouchUpInside)!

XCTAssertTrue(actions.containsObject("login"), "IBActions not found")


Related Topics



Leave a reply



Submit