Swiftでクラス拡張の単体テストを作成しようとしています。しかし、私は入れませんSwiftでクラス拡張の単体テストを書く
import XCTest
class AlertTest: XCTestCase {
func testAlert() {
let alert = presentAlert("Presented Alert", "This is an Alert!")
}
}
:私は以下のコードを含む私のユニットテスト用のファイルを作成し
extension UIViewController {
func presentAlert(title: String, message : String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
}
}
:クラス拡張自体は、以下のような指定されたタイトルとメッセージをUIAlert
を紹介します"Use of unresolved identifier 'presentAlert'"
のエラーです。
public func presentAlert(title: String, message : String)
まだ運:私はこのSO threadに相談した後、自分の内線にpublic
を追加してみました。誰もがいくつかの洞察力を持っている?これは、ビューコントローラで
import Foundation
protocol Presentable {}
extension UIViewController {
public func presentAlert(title: String, message : String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
}
}
、私は警告を表示する:@hkgumbsによって回答に基づいて
EDIT
が、これは私のアラート拡張のための私の現在のコードですまだ私のアラートを呼び出すための正しい方法でしょうか?
self.presentAlert("Invalid URL", message: "Please try again")
第二に、あなたのコメントに基づいて、これは私がダミーの値にPresentable
を呼び出すことによって理解何何ですが、SomethingPresentable
は何のメンバーPresentAlert
を持っていないとして、それが間違っています。私の理解に間違っているところ?
func testAlert() {
let app = XCUIApplication()
struct SomethingPresentable: Presentable {}
SomethingPresentable.presentAlert("Presented Alert", message: "This is an Alert!")
XCTAssert(app.alerts["Presented Alert"].exists)
app.alerts["Presented Alert"].tap();
}
EDIT 2 @hkgumbs、あなたの最新のコメントをもとに、これは私が拡張のために持っているものです。
import Foundation
protocol Presentable {}
extension Presentable {
func presentAlert(title: String, message : String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
}
}
そして、これは私からそれを呼び出すようにしようとしている方法ですViewController:
Presentable.presentAlert("Invalid URL", message: "Please try again")
しかし、私はタイプSelf
上のインスタンスメンバpresentAlert
の「使用のエラーを取得するには、あなたをしました代わりにタイプSelf
の変数を使用することを意味しますか?
次に、私はこのテストがどのように見えるかを推測していますか?
func testAlert() {
let app = XCUIApplication()
struct SomethingPresentable: Presentable {}
SomethingPresentable.presentAlert("Presented Alert", message: "This is an Alert!")
XCTAssert(app.alerts["Presented Alert"].exists)
app.alerts["Presented Alert"].tap();
}
あなたの拡張は 'UIViewController'にありますので、' UIViewController'でそれを呼び出す必要があります。あなたがそれを呼び出す方法は宣言にも一致しません。 – dan
@danそのタイポをキャッチしてくれてありがとう...私はちょうど質問でそれを修正しました。ユニットテストでViewControllerで呼び出すのはどうですか? – narner