在Swift中将测试添加到OSX项目时,无法访问主目标方法 [英] Unable to access Main Target Methods when Adding Tests to OSX Project in Swift
本文介绍了在Swift中将测试添加到OSX项目时,无法访问主目标方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经尝试添加一个测试包到我的项目,似乎已经成功。
但是,当我尝试在我的主项目中创建类的实例时,我无法看到它们。
项目似乎构建得很好,但我可以实例化任何测试对象
class EmailHelper: NSObject {
func generateEmailBody(greeting:String,bodyContent:String) - > String {
//内容在这里
返回消息
}
}
import XCTest
类MyProject_DesktopTests:XCTestCase {
override func setUp(){
super.setUp()
//将设置代码放在这里。在调用类中的每个测试方法之前调用此方法。
}
override func tearDown(){
//在这里放置teardown代码。此方法在调用类中的每个测试方法后调用。
super.tearDown()
}
func testExample(){
//测试会进入这里,但我似乎无法解析EmailHelper class-它生成一个lint错误
//这是一个功能测试用例的示例。
//使用XCTAssert和相关函数来验证测试是否产生正确的结果。
}
func testPerformanceExample(){
//这是一个性能测试用例的例子。
self.measureBlock {
//输入您要测量此处的时间的代码。
}
}
}
方案
我设法通过添加Testable到类的顶部(这似乎是OSX的具体问题)工作。
import XCTest
@testable import MyProjectName //< ---这是缺少的位.... :)
class MyProject_DesktopTests:XCTestCase {
override func setUp(){
super.setUp()
//在这里放置setup代码。在调用类中的每个测试方法之前调用此方法。
}
override func tearDown(){
//在这里放置teardown代码。此方法在调用类中的每个测试方法后调用。
super.tearDown()
}
func testExample(){
//测试会进入这里,但我似乎无法解析EmailHelper class-它生成一个lint错误
//这是一个功能测试用例的示例。
//使用XCTAssert和相关函数来验证测试是否产生正确的结果。
}
func testPerformanceExample(){
//这是一个性能测试用例的例子。
self.measureBlock {
//输入您要测量此处的时间的代码。
}
}
}
一定要清理你的项目添加它后,它似乎工作。
I have tried adding a testing bundle to my Project which seems to have succeeded.
However when I try to create instances of the Classes in my main project- I am unable to see them.
The Project seems to build fine - but I can' instantiate any of the test objects
Any ideas how to access them
Example Class to Test:
class EmailHelper: NSObject {
func generateEmailBody (greeting: String, bodyContent: String) -> String {
//Content goes in here
return message
}
}
import XCTest
class MyProject_DesktopTests: 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() {
// The Test would go in here but I can't seem to resolve EmailHelper class- it generates a lint error
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
// Put the code you want to measure the time of here.
}
}
}
解决方案
I managed to get it working by adding Testable to the top of the class( This appears to be OSX specific issue)
import XCTest
@testable import MyProjectName // <--- This was the missing bit.... :)
class MyProject_DesktopTests: 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() {
// The Test would go in here but I can't seem to resolve EmailHelper class- it generates a lint error
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
// Put the code you want to measure the time of here.
}
}
}
Also be sure to clean your project after adding it an it seems to work.
这篇关于在Swift中将测试添加到OSX项目时,无法访问主目标方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文