I want to create a function or convenience init of a class that can not be available for TestTarget when import with @testable import, I am not sure it’s possible but looking for any way to restrict it.
class A {
// Should not be accessible in Test Target
func foo() {
}
}
In Testing when @testable import it should not be available.
/**********
UPDATE
***********/
Problem statement
The Long param init method is used with convenience methods to provide default arguments but then in testing, I don’t want to access that convenience method with the default argument because it’s easy to forget to provide mock depedancy.
3
Answers
Mark that function as
Private
. Then it will not be available in test target.You cannot access private entities from another module and this also applies to test targets. That is what access control is for.
There are two solutions to do so.
init
With this solution, you can still use this
init
in your unit test, but it will crash at runtime.Testing
, you can do this bySelect your debug config -> Click + -> Duplicate Debug
, and rename it toTesting
then in your main target, add a new flag
TESTING
in the configTesting
.Next go to
Product -> Scheme -> Edit Scheme
Set build configuration toTesting
for yourTest
Finally add this around your
init
.Now, you should not be able to use this
init
in your unit test.You could just
import
the module as opposed to@testable import
.