Clean-Swift / CleanStore

A sample iOS app built using the Clean Swift architecture. Clean Swift is Uncle Bob's Clean Architecture applied to iOS and Mac projects. CleanStore demonstrates Clean Swift by implementing the create order use case described by in Uncle Bob's talks.
https://clean-swift.com/clean-swift-ios-architecture/
MIT License
1.92k stars 316 forks source link

Testing viewDidLoad without using Storyboards #17

Open premedios opened 7 years ago

premedios commented 7 years ago

If my UI was being created by code, how would I go about testing the output in the view controller?

kmmraj commented 7 years ago

You can do it something like below

class MyViewControllerTests: XCTestCase { // MARK: - Subject under test

var sut: MyViewController!
var window: UIWindow!

// MARK: - Test lifecycle

override func setUp() {
    super.setUp()
    window = UIWindow()
    setupMyViewController()
}

override func tearDown() {
    window = nil
    super.tearDown()
}

// MARK: - Test setup

func setupMyViewController() {
    sut = MyViewController.instance()
    loadView()
    addViewToWindow()
}

func loadView() {
    let _ = sut.view
}

func addViewToWindow() {
    window.addSubview(sut.view)
    RunLoop.current.run(until: Date() as Date)
}

// MARK: - Test doubles

// MARK: - Tests

...

}