如何从Swift项目中的测试中访问我的应用程序代码? [英] How do I access my app code from my tests in a Swift project?

查看:205
本文介绍了如何从Swift项目中的测试中访问我的应用程序代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Swift Xcode项目,其代码如下:

  class Utils:NSObject {
class func cleanString (input:String,trim:Bool) - > String {
// ...
}
}

然后我尝试测试它:

  import XCTest 

class AppTests:XCTestCase {
func testConfiguratio(){
Utils.cleanString(foo,trim:true)
}
}

但我得到这个错误:

  / Users / pupeno / Projects / macninja / AppTests / AppTests.swift:35:9:使用未解析的标识符'Utils'

启用的API:



我错过了什么?

解决方案

库代码和测试代码是2个不同的模块。因此,您必须将库导入测试代码,并使您想要测试的函数公开,例如:

  public class Utils:NSObject {
public class func cleanString(input:String,trim:Bool) - > String {
// ...
}
}

  import XCTest 
import Utils

class AppTests:XCTestCase {
func testConfiguratio(){
Utils.cleanString(foo,trim:true)
}
}

如果您想查看工作代码,请查看我的IBANtools库项目< a>它实现了这个场景(类函数,swift框架,大量的测试)。


I have an Swift Xcode project with code such as:

class Utils: NSObject {
    class func cleanString (input: String, trim: Bool) -> String {
        // ...
    }
}

and then I try to test it:

import XCTest

class AppTests: XCTestCase {
    func testConfiguratio() {
        Utils.cleanString("foo", trim: true)
    }
}

but I get this error:

/Users/pupeno/Projects/macninja/AppTests/AppTests.swift:35:9: Use of unresolved identifier 'Utils'

I have Host Application APIs enabled:

What am I missing?

解决方案

As it has been said already, the library code and the test code are 2 different modules. So you have to import the library into the test code and also make the functions that you want to test public, e.g:

public class Utils: NSObject {
    public class func cleanString (input: String, trim: Bool) -> String {
        // ...
    }
}

and

import XCTest
import Utils

class AppTests: XCTestCase {
    func testConfiguratio() {
        Utils.cleanString("foo", trim: true)
    }
}

If you want to see working code look at my IBANtools library project which implements exactly this scenario (class functions, swift framework, lots of testing).

这篇关于如何从Swift项目中的测试中访问我的应用程序代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆