Tests with Quick and Nimble - ios

I installed Quick and Nimble frameworks for tests in Swift. But inside test class my class' types is unresolved. In the top of test class I make imports:
import UIKit
import Nimble
import Quick
import MyProject
I know my main target should be at target dependencies and no classes files (except test classes) should be added to compile sources of test target.
Why my files is not visible in test target?

In XCode 7, you can include internal ivars with a line:
#testable import
No need to make the ivars public if you want to keep them conceiled from the outside world...

You need to declare your classes as public. Otherwise, you won't see anything from your test bundle
Also, the default access level of every property/ function is just its own target. So you also need to declare them public

Related

Instantiated a struct which is a part of iOS framework and has internal access control

I would like to instantiate a struct which is a part of third party iOS framework and cannot be instantiated from outside means has internal init. But I need to create for Unit testing. Any idea how to achieve this?
As Writing Test Classes and Methods documentation states:
Xcode provides a two-part solution to this problem:
When you set the Enable Testability build setting to Yes, which is true by default for test builds in new projects, Xcode includes the
-enable-testing flag during compilation. This makes the Swift entities declared in the compiled module eligible for a higher level of access.
When you add the #testable attribute to an import statement for a module compiled with testing enabled, you activate the elevated access
for that module in that scope. Classes and class members marked as
internal or public behave as if they were marked open. Other entities
marked as internal act as if they were declared public.
#testable import MySwiftFramework

#testable import moduleName not importing everything

I have a project that is mixed Obj-C and Swift and I am having some issues getting my unit tests to work.
I'm using the #testable import moduleName directive to import my files, however it does not appear to be importing all the files. I have full access to pretty much all of my Obj-C models, manager classes etc. but none of the view controllers (95% of which are in Obj-C) are available from within the XCTestCase, along with all of my Swift files.
I have imported #testable import ModuleName at the top of the XCTestCase,
I have also edited the target to enable testability, but the only way I can access these files seems to be if I set the files target membership manually, which if I have understood everything correctly, I should not need to do.
Any suggestions?
Yeah I just also went from the same problem about the Unit testing with the project having both Objective-C & Swift languages.
So basically what i found was
You have to add the respective file and all the required files to Test target. And also required to add them to the Bridging_Header to access those files
Moreover the reason behind using the
#testable
is to test methods internal .... methods.
#testable import moduleName
This is used for the purpose of visibility of methods, i.e like internal methods can now be visible in unitTest but atill private methods won’t.

Whats the alternative of #testable import in Objective C

I am working with Unit Testing in Xcode using XCTest provided by Xcode in objective C.
I know how to import Module in Swift like below.
#testable import AppName
Whats the alternative in objective C.
#testable overrides access rights in Swift, allowing you to test internal methods in unit tests.
Objective-C has no such access modifiers, therefore you don't need #testable and you just import the module normally.
If you need to unit test internal Swift methods, you will have to write your tests in Swift.
In Objective C you can simply #import them, as there are no such "internal" method access limitations as in Swift.
Also, On Xcode 6 your main target should be already linked to the test target. If not, try to check the "Allow testing Host Application APIs" checkbox inside Your Test Target > General > Testing. Take a look at this question for more information.

Creating pod, use of undeclared type for my pod classes

I'm creating a pod in swift to use with cocoapods, but I'm having a strange problem when trying to use it.
My pod is downloaded normally after 'pod install' and I can import it via 'import MyPod'. So far so good, but when I try to access my classes in the pod, I get the message
"Use of undeclared type 'NameOfTheClassIwantToUse'".
If i go to my pod folder in Pods project, all the files are there, but I cannot use it.
I also noticed when I go into the import 'MyPod' with command+click I can see just few imports instead of all my classes, like:
import MyPodProjectName
import MyPodProjectName.Swift
import Foundation
import UIKit
public var MyPodProjectNameVersionNumber: Double
While other imports has all the classes that are supposed to be available upon importing.
Does anyone has a clue on what am I missing here?
Be sure to declare you class as public, as stated here
https://guides.cocoapods.org/making/using-pod-lib-create
As stated there:
It's worth mentioning here, as this catches people quite often, a Swift library needs to have its classes declared as public for you to see them in your example library.
So, just declare namespace "public" on you classes and you are good to go
I had the same problem, and this worked for me!
Yes you need to declare your class, methods and relevant variables as public and don't forget to import your module on top of the class you need.

Cannot see Swift classes from test target

I have an iOS app using both ObjC and Swift code. I tried setting up a test target for it today with no success. I have a single test case class written in Swift. I imported my app's module there. I made sure the classes I am trying to access are public. But I cannot see them from my test target. I can see ObjC code from there though.
I tried the exact same steps on a dummy project and there it worked fine.
I don't want to add the classes for testing to the test target's compile sources. I am also using Xcode 6.3 and updating to Xcode 7 is not an option for me at the moment.
Any ideas on what I am doing wrong?
Make sure your import is marked with a #testable annotation. For example, you want:
#testable import myprojectname
as opposed to:
import myprojectname
The issue went away after deleting and recreating the test target.

Resources