0

I have a unit test that needs to access internal methods/properties on a module imported by my application target.

E.g.

SubModule.swift

public class SubModuleType {
    ...
    internal let value: InternalSubModuleType
    ...
}

AppViewController.swift

import SubModule
// do things with SubModuleType

AppViewControllerTests.swift

@testable import App
@testable import SubModule

func testWithSubModule() {
    let internalSubModuleTypeInstance = SubModule.SubModuleType().value
    // ... run a test dependent on internalSubModuleTypeInstance
}

In this test I receive 'Use of undeclared type 'InternalSubModuleType'' when accessing .value.

  • I have added the SubModule target to App-Tests "Target Dependencies"
  • I have set "Enable Testability" to YES for both the App target and SubModule target for the scheme I'm compiling for testing.

@testable import is supposed to allow you to access types marked internal under these conditions. I'm not sure why I'd be receiving this compiler error. I can still use any type that is marked internal in my App target by using @testable but not my SubModule target.

Are you only allowed 1 target to be @testable import in a test target or is there something I'm missing?


using Xcode 9, Swift 3.2

Awesome-o
  • 2,002
  • 1
  • 26
  • 38
  • I'm seeing something similar (not exactly the same) https://stackoverflow.com/questions/46600071/xcode-9-not-linking-indexing-correctly Have you tried something to force an index to happen and see if resolves it by any chance? This isn't an answer, just seeing if there's a relationship. – ad-johnson Oct 06 '17 at 12:04
  • I've tried quitting Xcode, deleting derived data, reopening and recompiling numerous times and it doesn't work. When I delete derived data it reindexes. This does look very similar however, wonder if it's a bug in Xcode 9. This setup on this project is new though (post Xcode 9 release) so I have nothing to compare against. – Awesome-o Oct 06 '17 at 22:07
  • I have tried the following which has improved it. I suspect part of the problem, for me, is that Xcode works in a manner different to what I think it should in my head. - Make changes in App Target - Save each source file as I go along - CMD-S - Build app - Switch to Unit Test Target - Make changes, saving files as I go along. This seems to pick up app changes but not always straightaway. It could be indexing slowly in the background (very slowly, it's a tiny app!) - Build - this seems to tidy up any lingering error messages - Run tests As you say, I suspect the issue is different. – ad-johnson Oct 07 '17 at 10:03

1 Answers1

0

Recreating the Testing target seemed to have done the trick for me.

Simply delete your old Testing target, create a new one and add this target to all the testing files that you have.

streem
  • 9,044
  • 5
  • 30
  • 41