在以下代码中,如果我注释掉分配给view属性的变量,则测试将失败。我所指的行是:
_=sut.view
但是,如果不注释该行代码,则测试将通过。为什么甚至有必要?
这是完整的单元测试:
import XCTest
@testable import ToDo
class ItemListViewControllerTests: XCTestCase {
var sut:ItemListViewController!
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
sut = storyboard.instantiateViewControllerWithIdentifier("ItemListViewController") as! ItemListViewController
_=sut.view
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func test_TableViewIsNotNilAfterViewDidLoad(){
XCTAssertNotNil(sut.tableView.dataSource)
XCTAssertTrue(sut.tableView.dataSource is ItemListDataProvider)
}
func testViewDidLoad_ShouldSetTableViewDelegate(){
XCTAssertNotNil(sut.tableView.delegate)
XCTAssertTrue(sut.tableView.delegate is ItemListDataProvider)
}
func testViewDidLoad_ShouldSetDelegateAndDataSourceToSameObject(){
XCTAssertEqual(sut.tableView.dataSource as? ItemListDataProvider, sut.tableView.delegate as? ItemListDataProvider)
}
}
在第一次view
访问属性之前,视图控制器不会加载其视图,因此将view
赋给变量将加载它。
如果没有加载视图则没有网点将被迷上了这样sut.tableView
就会nil
和你的测试将会失败。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句