5

I created a new Single view Swift project and create a MyModel:

class MyModel {

    func add(a : Int, b : Int) -> Int {
        return a + b
    }
}

But I had a problem to create a test case. Error message:

/Volumes/Macintosh HD/Users/user/Projects/TestCase/TestCaseTests/TestCaseTests.swift:26:19: Use of unresolved identifier 'model'

enter image description here

As you see, import TextCase (my target) didn't solve the problem. The only way to solve the problem is to add MyModel.swift to target: TestCaseTests. But this is different from Objective-C project (I don't need to add the .m files to test case target at all). Is this a bug or a design?

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
  • Did you add the file to the the test case? See... https://medium.com/swift-programming/swift-testing-privates-or-rather-internals-9a3ac5a8a501 – kasplat Jul 24 '14 at 04:01

3 Answers3

17

You should add the swift file you are testing to the testing target.

This can be done by clicking on the swift file, going to the Utilities panel (the one on the right) and checking the checkbox under "Target membership".

add testing target to swift file

No need to change the access modifier to public, internal will do.


UPDATE

As of XCode 7 there is no need to make any file member of the testing target any more.

The recommended way is to use @testable import {Product Module Name}. Just make sure to use the product module's and not the projects' folder name.

Filip Hermans
  • 272
  • 1
  • 4
5

As per this answer, you can use @testable import {Module Name} now (with Xcode 7).

Community
  • 1
  • 1
Julian
  • 2,837
  • 17
  • 15
  • You should quote the full answer, especially since you don't link to the answer directly, but the question (leaving us to read/guess what you're referring to) – Machavity Oct 07 '15 at 18:19
  • This is the new correct answer. However, there should be more information on how to Testability now works. – Scott H Oct 23 '15 at 17:10
4

Access modifiers had become available starting Beta 4, and the unit test class is now considered as outside the subject's module, so for it to access anything within your module, it has to be declared public.

public class MyModel {

    public func add(a : Int, b : Int) -> Int {
        return a + b
    }

}
undetected
  • 474
  • 3
  • 12
  • 1
    It doesn't work even I add ***public***. The only solution by far is to add to my tests target. – Bagusflyer Jul 24 '14 at 06:33
  • @bagusflyer did you import the ProjectName where MyModel() sits into your unit test class? – TheLazyChap Jul 29 '14 at 06:42
  • I tried to import ProjectName.(The screenshot doesn't show this case). It didn't help. The only way is to add MyModel to my TestCase Target. – Bagusflyer Jul 30 '14 at 07:26
  • 1
    Thank you! Head is sore from banging againt wall. – Tim Scott Aug 08 '14 at 18:45
  • 1
    So is there a better solution without adding MyModel.swift to the test target? This approach doesn't work with model because when the model class is added to the test target, the model would pick up the wrong target as namespace. – Sam Aug 12 '14 at 16:29