Created
November 28, 2017 10:29
-
-
Save muizidn/91b93a22f40f67fafdabe891943c16e0 to your computer and use it in GitHub Desktop.
Saya ingin membuat subclass Mock untuk ABCViewController namun selalu error saat runtime. SIGNAL SIGBART
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Di kode produksi ABCApp | |
class ABCViewController: UIViewController { | |
func foo() -> String { | |
return "Jalankan kode PRODUKSI untuk ABCViewController" | |
} | |
} | |
// Di ViewControllerTests | |
@testable import ABCApp | |
class ABCViewControllerTests: XCTestCase { | |
class ABCViewControllerMock: ABCViewController { | |
override func foo() -> String { | |
return "Jalankan kode TEST untuk ABCViewController" | |
} | |
} | |
var abcVC: ABCViewController! | |
func setup() { | |
super.setup() | |
abcVC = UIStoryboard(name: "Main", bundle: Bundle.main) | |
.instantiateViewController(withIdentifier: "ABCViewController") as! ABCViewController | |
_ = abcVC.view | |
} | |
func testFooProduceTESTCode() { | |
let abcVCMock: ABCViewControllerMock = abcVC as! ABCViewControllerMock | |
let result = abcVCMock.foo() | |
XCTAssertEqual(result, "Jalankan kode TEST untuk ABCViewController") | |
} | |
} | |
//Saya ingin agar kode dalam foo() saat testing yang dieksekusi adalah kode dari ABCViewControllerMock. | |
//Tantangannya adalah saat runtime abcVC tidak bisa di cast ke ABCViewControllerMock. (Saya memahami alasan tidak bisa cast-nya) | |
//Yang saya ingin tanyakan adalah : Adakah pendekatan yang lebih baik untuk melakukan mock ViewController? | |
//P.S: Saya masih baru dalam dunia testing-testing jadi mohon bantuannya juga untuk meluruskan apa itu mock, stub, dll. Mungkin jika salah istilah. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment