iOS Swift unit test result in unresolved identifier - ios

Simple function in swift for testing:
func testExample() {
var tagname = "someClass()"
var logger = Device("", "") //unresolved identifier
XCTAssert(true, "Pass")
}
Even after I import my module with "import ", I still cannot use classes from my module. Also though I might have messed something up in the project, but NONE of my sample projects will let me use module classes.
Seems like it should work but might have broken in beta 2.
EDIT: fixed it
The IDE didn't pick up the check for the param names. Seems Xcode is still a tad iffy

I hit the same issue today, not sure if this is only available recently - rather than using import TARGET_NAME in your test file and/or declaring your classes/methods as public, you can add your file to your Tests target via XCode's File Inspector.
Cmd + Opt + 1 to show it while in any file, and check the box under Target Membership for your Tests Target.
You may need to rebuild.... Cmd + b.
Should this be specific to my system.... I'm running Xcode 6.3 beta 1, and testing via Quick + Nimble, both installed with the latest cocoapods beta.

The problem for me was that I had non-alphanumerical characters in my main target name.
I had to import it the following way (Note the special #testable annotation)
#testable import my_tutorial_app

It seems that in Xcode 6 Beta 4 you need to declare public classes and methods as "public". Example:
public class Device {
public init(...) {
}
public func myMethod(...) {
}
}
Now they are accessible from the swift test class.

I had the same problem and discovered that it works if I call it like this:
SuperStructName.StructName

Related

Why can't Xcode find my App for Unit Tests?

I'm fairly inexperienced in iOS. I wanted to write Unit Test for my Cocoapods Project.
My ViewControllers that I want to test are in Pods. When I want to write a unit test, I already receive an error message with
import MyApplication
which says
No such module 'MyApplication'
Note that I used "MyApplication" only as a substitute for orginal name I crossed out in red in the picture.
What is the issue?
EDIT:
code of my test class:
import UIKit
import XCTest
import Pods_MyApplication_Example
#testable import Pods_MyApplication
class Tests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
XCTAssert(true, "Pass")
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure() {
// Put the code you want to measure the time of here.
}
}
}
To create a test target, on the test navigator, I click on the '+' and then choose 'New Unit Test Target'
I am sorry if there are any information missing. I will add those if there are any!
EDIT2: Changed picture which might provide more information
You must use #testable import to import your app's module for unit testing. You have to supply the name of your application target, Pods_MyApplication_Example, not the name of the project. In your example it will look like the following:
#testable import Pods_MyApplication_Example
And remove the import statement that imports the app target.
Thanks for the responses. I had to add the Plugin/Application that I want to test to the Test Target in my Podfile. So the Podfile looks like this:
use_frameworks!
target "MyApplication_tests" do
pod 'MyApplication', :path => '../'
end
After running pod update I could then import and test my Application.
This error can also occur when you don't have the correct test host appear.
If you see this, and you have selected the iOS Simulator or iPadOS simulator. Make sure that your test target supports that platform.
Select the Xcode project in the lefthand browser.
Click on your test target in the Project's General tab.
Under "General" select your appropriate host application under
"Testing".

Use of undeclared type 'XCTestCaseEntry'

Hi I can't seem to figure out why I am Use of undeclared type 'XCTestCaseEntry'
when using the following code in my .xctest:
import XCTest
#if !os(macOS)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(TrakkproTests.allTests),
]
}
#endif
I just ran into the same issue while attempting to run unit tests for a custom framework I've built using the Swift Package Manager.
It turned out I had accidentally selected an iOS simulator in the Xcode scheme/device selector:
This compiled the code for iOS which satisfies the compilation condition of !os(macOS) and causes the error to occur. XCTestCaseEntry appears to be part of the swift-corelibs-xctest project which provides XCTest functionality to non-Apple platforms (i.e. linux) but is not the same implementation Apple uses... I think.
The solution for me was to select "My Mac" in the scheme/device selector:
looks like the typealias XCTestCaseEntry is added as part of the commit https://github.com/apple/swift-corelibs-xctest/commit/1c7fb283231ce53960a232aa7c771bb2d38dee62 on oct 2017.
I don't think the XCTestCase class as part of the XCode/XCTest has this change yet. Where are you checking this anyway ?

Swift: import framework but can't find class in it

I import a swift framework to a swift project, but when I call the class in this framework, Xcode throw a compile error "Use of undeclared type 'xxxx(class name)' ".
I feel Xcode have found the framework, otherwise it will complaint "can't find xxx(framework name)".
But why Xcode can't find the class of this framework.
I have tried remove and re-add framework, and delete DeivedData files, but all of them not work. I haven't use CocoaPods to import framework.
Any idea?
In a Framework that was built for Release (and not Debug), the class symbols need to be accessible.
Make sure you are you trying to access Public or Open classes from your Swift framework.
// set the Framework class to Public
public class rnHello{
// set the initializer to public, otherwise you cannot invoke class
public init() {
}
// set the function to public, as it defaults to internal
public static func world() {
print("hello from a static method")
}
}
Now you can access this via your Swift code or if you attach lldb using:
lldb) po rnHello.world()
Make sure that the FrameWorkSearch path in the BuildSettings of the project is reflecting the correct path to your framework.

Xcode Test not detect my class

I have class named Meal.swift in my project and a unit test
func testMealInitialization() {
// Success case.
let potentialItem = Meal(name: "Newest meal", photo: nil, rating: 5)
XCTAssertNotNil(potentialItem)
// Failure cases.
let noName = Meal(name: "", photo: nil, rating: 0)
XCTAssertNil(noName, "Empty name is invalid")
}
But the problem is that: Use of unresolved identifier "Meal"
#testable import MyApp should work fine. Just remember to set appropriate configurations within Debug for your UITest target.
Xcode 7 adds the #testable import statement to simplify unit testing. At the top of your unit test class files, add the following statement:
#testable import MyApp
Where MyApp is the name of your iOS app. Now the unit test target should be able to find the classes in your app and run the tests. If you get link errors saying that Xcode cannot find your app classes, make sure the Product Module Name build setting's value matches the name you use in the #testable import statement, MyApp in this example.
If #testable import does not work for you, a workaround is to make your app classes members of the unit test target. You can set the target membership of a file in Xcode using the file inspector.
I also encountered this issue, what worked for me is reloading my test file and retyping the
#testable import FoodTracker
then at that point, it detected my FoodTracker classes (Meal class) and errors are gone.
Click on your Meal class. Then on the right side you'll see 'Target Membership' section. Select your test project.
(Xcode 7)
VoilĂ .
In my case, I got error only in the new class I've just made, and it makes me confuse. So, it works by select Unit Test target under class membership of my new class. Or delete the class, make new class again, then select Unit Test Target in that new class.

'Use of Unresolved Identifier' in Swift

So I have been making an app, and everything has been working great. But today I made a new class like usual and for some reason in this class I can't access Public/Global variable from other classes. All the other classes can, but now when ever I try to make a new class I can't. How would this be fixed?
I am using Swift and Xcode 6.
Working Class:
import UIKit
import Foundation
import Parse
import CoreData
var signedIn = true
class ViewController: UIViewController {
New Class:
import UIKit
class NewClass: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
signedIn = false
}
But on signedIn = false
I get the error:
use of unresolved identifier "signedIn"
One possible issue is that your new class has a different Target or different Targets from the other one.
For example, it might have a testing target while the other one doesn't. For this specific case, you have to include all of your classes in the testing target or none of them.
Once I had this problem after renaming a file. I renamed the file from within Xcode, but afterwards Xcode couldn't find the function in the file. Even a clean rebuild didn't fix the problem, but closing and then re-opening the project got the build to work.
'Use of Unresolved Identifier' in Swift my also happen when you forgot to import a library. For example I have the error:
In which I forgot the UIKit
import UIKit
Sometimes the compiler gets confused about the syntax in your class. This happens a lot if you paste in source from somewhere else.
Try reducing the "unresolved" source file down to the bare minimum, cleaning and building. Once it builds successfully add all the complexity back to your class.
This has made it go away for me when re-starting Xcode did not work.
Another place I've seen this error is when your project has multiple targets AND multiple bridging headers. If it's a shared class, make sure you add the resource to all bridging headers.
A good tip to is to look in the left Issue Navigator panel; the root object will show the target that is issuing the complaint.
For me this error happened because I was trying to call a nested function. Only thing I had to do to have it fixed was to bring the function out to a scope where it was visible.
In my case, I had an Object-C file which was also in the same Target Membership. I fixed by adding #import "YourObjectCFileHeader.h" inside file Bridging-Header.h
Because you haven't declared it. If you want to use a variable of another class you must use
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : ViewController = segue.destinationViewController as ViewController
DestViewController.signedIn = false
}
You have to put this code at the end of the NewClass code
Your NewClass inherits from UIViewController. You declared signedIn in ViewController. If you want NewClass to be able to identify that variable it will have to be declared in a class that your NewClass inherits from.
I got this error for Mantle Framework in my Objective-Swift Project.
What i tried is ,
Check if import is there in Bridging-Header.h file
Change the Target Membership for Framework in Mantle.h file as shown in below screenshot.
Toggle between Private Membership first build the project , end up with errors.
Then build the project with Public Membership for all the frameworks appeared for Mantle.h file, you must get success.
It is just a bug of building with multiple framework in Objective-C Swift project.
If this is regarding a class you created, be sure that the class is not nested.
F.e
A.swift
class A {
class ARelated {
}
}
calling var b = ARelated() will give 'Use of unresolved identifier: ARelated'.
You can either:
1) separate the classes if wanted on the same file:
A.swift
class A {
}
class ARelated {
}
2) Maintain your same structure and use the enclosing class to get to the subclass:
var b = A.ARelated
I did a stupid mistake. I forgot to mention the class as public or open while updating code in cocoapod workspace.
Please do check whether accesor if working in separate workspace.
You forgot to declare the variable. Just put var in front of signedIn = false
My issue was calling my program with the same name as one of its cocoapods. It caused a conflict. Solution: Create a program different name.
This is not directly to your code sample, but in general about the error. I'm writing it here, because Google directs this error to this question, so it may be useful for the other devs.
Another use case when you can receive such error is when you're adding a selector to method in another class, eg:
private class MockTextFieldTarget {
private(set) var didCallDoneAction = false
#objc func doneActionHandler() {
didCallDoneAction = true
}
}
And then in another class:
final class UITextFieldTests: XCTestCase {
func testDummyCode() {
let mockTarget = MockTextFieldTarget()
UIBarButtonItem(barButtonSystemItem: .cancel, target: mockTarget, action: MockTextFieldTarget.doneActionHandler)
// ... do something ...
}
}
If in the last line you'd simply call #selector(cancelActionHandler) instead of #selector(MockTextFieldTarget.cancelActionHandler), you'd get
use of unresolved identifier
error.

Resources