Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing viewDidLoad without using Storyboards #17

Open
premedios opened this issue Feb 17, 2017 · 1 comment
Open

Testing viewDidLoad without using Storyboards #17

premedios opened this issue Feb 17, 2017 · 1 comment

Comments

@premedios
Copy link

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

@kmmraj
Copy link

kmmraj commented Feb 17, 2017

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

...

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants