3

I want to use objective C class in my swift tests.

  • My project is initially set up in objective c.
  • I have bridging header set up already working for my project.
  • I have import statement already in place in bridging header for the file, which I want to use in tests.
  • Search paths for test target are also set to same bridging header, although I've tried using separate bridging headers also.
  • I use cocoa pods so have a xcworkspace file.
  • Pod file has inherit search path included for tests, though I am not using pod frameworks in tests.

I get 'Use of undeclared type 'SySearchProductRequest'' error while trying to use the class. SySearchProductRequest is the objc class I want to use.

I am using @testable import Mymodulename to import my module which has this class, in tests.

I am stuck with an approach of writing objc test cases, which I don't want to do. Is there a way or is there something I am doing wrong or missing to make this work.

pk75
  • 511
  • 7
  • 18
  • http://codesheriff.blogspot.com/2015/05/importing-swift-code-from-objective-c.html doesn't work either. – pk75 Oct 10 '18 at 09:04
  • I tried adding $CONFIGURATION_TEMP_DIR/MyProject.build/DerivedSources in my test target to include Derived sources in my HEADER SEARCH PATHS, It seems to have solved the issue although I was trying multiple things so not sure about this. – pk75 Oct 11 '18 at 07:59
  • 1
    Tried this and it doesn't work for me because none of the Objective-C header files are copied to $CONFIGURATION_TEMP_DIR/MyProject.build/DerivedSources. – drekka Oct 07 '19 at 00:25

1 Answers1

0

Just check if your class is added to Test Targets in Target Membership in File Inspector Example Screenshot. and Then Build the project again . Hope It removes the error .

Saurabh
  • 222
  • 1
  • 7
  • Hey @Saurabh I use testable import to resolve paths, so this step is not needed. Thanks – pk75 Oct 10 '18 at 09:06
  • Saurabh, I strongly discourage adding production code to test targets. While "it works," it's not correct. – Jon Reid Oct 11 '18 at 17:14
  • OK @Jon Reid . I will think about it and try to find better solution for the problem. Thanks . – Saurabh Oct 12 '18 at 10:22
  • @JonReid Can you please expand on you discouraging of adding production code to test target ? How are you going to test your production code unless it is added to the test target ? – Petar Jul 21 '20 at 16:02
  • @Petar When set up correctly, the test target links against the production code target. Except for things declared private, everything is accessible with @ testable import. – Jon Reid Jul 22 '20 at 03:17