guru
guru

Reputation: 129

Can't access swift files in Unit test target in Xcode

Adding swift files to test target will work, but it is not the best way to do. My problem is I can't able to access Swift file whereas Objective-C files are accessible.

I have checked product module name and set configuration file same as project file for test target. Even removed the test target and readded, but, still encountering "Use of undeclared type in SlideViewController".

Can anyone help me with solving this issue?

Upvotes: 9

Views: 5675

Answers (3)

Fernando
Fernando

Reputation: 27

Sometimes you will need to add files to the build phases of your test target.

1.- Go to project navigator

2.- Select your project

3.- On the project and target list, select the target for your tests (Ex. "MyProjectTests")

4.- Select Build Phases tab

5.- Open "Compile Sources"

6.- Using the plus sign, add the files needed for the compilation

Upvotes: -1

N Brown
N Brown

Reputation: 557

By default you won't be able to access internal classes from your unit test target.

The apple docs on writing tests with swift say that you need to take two steps to get around this:

  1. Set the ENABLE_TESTABILITY build setting to YES.
  2. Add @testable to the import statement for your module. @testable import MySwiftApp

If you follow both of those steps your SlideViewController (as long as it is not a private class) should be accessible from your unit test file as if it was declared as an open class.

Upvotes: 13

Max
Max

Reputation: 326

Try to add this on top of your test file:

@testable import <YOUR_MODULE_NAME>

Upvotes: 2

Related Questions