Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test can't find class to test

Tags:

swift

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?

like image 346
Lee Avatar asked Jun 13 '14 19:06

Lee


2 Answers

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
like image 131
James Richard Avatar answered Oct 05 '22 09:10

James Richard


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

like image 28
Tung Fam Avatar answered Oct 05 '22 07:10

Tung Fam