Adrian
Adrian

Reputation: 20058

How to write unit tests for a protocol in Swift

The protocol is used by a custom view controller.
I am not sure if this is the correct way to go, but right now I am instantiating the view controller inside the unit test class.

Currently I am trying to do this:

override func setUp() {
    super.setUp()

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    vc = storyboard.instantiateViewController(withIdentifier: "Transactions") as! TransactionsViewController
}

But it says Use of undeclared type 'TransactionsViewController'.

Is this the way to go ? If yes, how do I make the view controller visible ?

Upvotes: 0

Views: 1193

Answers (2)

ecant
ecant

Reputation: 31

Since Swift 2, you can import the module you're trying to write tests for with:

@testable import ModuleName

With this method, you do not need to manually add files from your App Target to your Test Target.

So, if your main target is named "MyApp" and your test target is "MyAppTests", you can just include this at the top of each test file you're writing:

@testable import MyApp

Upvotes: 0

Sander
Sander

Reputation: 1375

Unit tests are usually a separate target, so to make files from your main target visible to the test targets you have to change their Target membership in the File Inspector.

enter image description here

In your case your should share both Main.storyboard and TransactionsViewController to your test target.

Upvotes: 2

Related Questions