官术网_书友最值得收藏!

Red - example 1

Open FirstDemoTests.swift, and add the following code to the FirstDemoTests class:

func test_MakeHeadline_ReturnsStringWithEachWordStartCapital() { 
  let viewController = ViewController() 
   

  let string = "this is A test headline" 
   

  let headline = viewController.makeHeadline(from: string) 
} 

This isn't a complete test method yet because we aren't really testing anything. The assertion is missing. But we have to stop writing the test at this point because the compiler complains that Value of type 'ViewController' has no member 'makeHeadline'.

Following the TDD workflow, we need to add code until the compiler stops printing errors. Remember that code does not compile within a test means the test is failing. And a failing test means we need to write code until the test does not fail anymore.

Open ViewController.swift, and add the following method to the ViewController class:

func makeHeadline(from string: String) { 
   
} 

The error still remains. The reason for this is that we need to compile to make the test target aware of this change. Run the tests to check whether this change is enough to make the test green again. The test is indeed green, but sometimes the error is still shown. The reason is that Xcode sometimes "forgets" to remove old errors.

Now we get a warning that the headline constant isn't used, and we should change it to _. So, let's use it. Add the following assert function at the end of the test:

XCTAssertEqual(headline, "This Is A Test Headline") 

This results in another compiler error:

Argument type '()' does not conform to expected type 'Equatable'

The reason for this error is that the makeHeadline(from:) method at the moment returns Void or (). But XCTAssertEqual can only be used if both expressions conform to the protocol Equatable and are of the same type. This makes sense as two expressions of different types can't be equal to each other.

Go back to ViewController, and change makeHeadline(from:) to this:

func makeHeadline(from string: String) -> String { 
  return "" 
} 
主站蜘蛛池模板: 贵定县| 达拉特旗| 封开县| 石河子市| 德钦县| 泸溪县| 大余县| 昌图县| 鄂温| 台北县| 武隆县| 沂水县| 康马县| 兰溪市| 敦煌市| 赤壁市| 阿勒泰市| 桂林市| 沁水县| 巩留县| 瑞金市| 合江县| 五家渠市| 广饶县| 宁明县| 鄂托克旗| 襄樊市| 杭锦旗| 德安县| 古丈县| 耒阳市| 西林县| 沾化县| 汨罗市| 泰宁县| 建瓯市| 禹州市| 旺苍县| 苍梧县| 安吉县| 衡水市|