Lee
Lee

Reputation: 4046

Unit test can't find class to test

I've got a stock standard class call GeoRssParserDelegate which needs to be tested.

In my swift unit test I've got this:

func testParser()
{
    var bundle = NSBundle(forClass:GeoRssParserTest.classForKeyedArchiver())
    var path = bundle.pathForResource("test", ofType: "xml")
    let data = NSData.dataWithContentsOfFile(path, options: NSDataReadingOptions.DataReadingMapped, error: nil)
    let xmlParser = NSXMLParser(data:data)
    let delegate = GeoRssParserDelegate() <-- Compiler fails here
    var bStatus = xmlParser.parse()
    XCTAssertTrue(bStatus, "Parse failed", file: __FILE__, line: __LINE__)        
}

The compiler fails on the line highlighted above. The compiler error is Use of unresolved idenitifier GeorRssParserDelegate

This class does exist and builds with the product itself. Is anything special required?

Upvotes: 42

Views: 22545

Answers (8)

alitosuner
alitosuner

Reputation: 1034

For me the reference or value type that I wanted to test is not marked as public. That's why I've just used as copy-paste for the associated type.

Upvotes: 0

Tung Fam
Tung Fam

Reputation: 8167

Solution for Xcode 8 and UP which works for me:

@testable import Product_Module_Name

note: not the target name but product's name.

Regarding answers above: making without @testable will require to make classes and methods public which changes the design of the app architecture. if you don't want to change it better to use this solution so you won't need to make changes to class being public or no.

Many thanks to this answer

Upvotes: 44

atereshkov
atereshkov

Reputation: 4565

@testable import MyApp

IMPORTANT! Note: MyApp must be the product module name in your project (Settings -> Target -> Build Setting -> Product Module Name)

Upvotes: 11

tania_S
tania_S

Reputation: 1370

Do the following 3 steps --> 1. Write @testable import Your_Project_name 2. Then save (CTRL+S) 3. Then build (CTRL+B)

Upvotes: 0

James Richard
James Richard

Reputation: 1535

You have to import your application's module into your unit test. This is usually your app name. For example, if your app name is GeoRssParser the import statement would be:

import GeoRssParser

Upvotes: 60

Lukasz Czerwinski
Lukasz Czerwinski

Reputation: 15482

If you're using brand new XCode 7 (as of Jul 21, 2015), simply write: @testable import GeoRssParserDelegate

Source: http://natashatherobot.com/swift-2-xcode-7-unit-testing-access/

Upvotes: 15

Just add the class you are testing to Target -> Build Phases -> Compile Sources

It's how I fixed my problem.

Upvotes: 2

Sophie Alpert
Sophie Alpert

Reputation: 143204

If the class you're testing is compiled into another target (such as your application, as opposed to the test bundle), make sure that the class or struct you're trying to use is marked public -- the default access visibility doesn't allow for cross-target testing.

Upvotes: 20

Related Questions