go test# go-tdd-example/perimeter [go-tdd-example/perimeter.test]
./main_test.go:28:23: not enough arguments in call to Perimeter
have (Rectangle)
want (float64, float64)
FAIL go-tdd-example/perimeter [build failed]
可以看到在測試的過程中我們看到 Error ,這個 Error 是告訴我們
not enough arguments in call to Perimeter
have (Rectangle)
want (float64, float64)
FAIL go-tdd-example/perimeter [build failed]
補充main package
原因是我們 Perimeter function ,定義的是 (width, height float64) 現在需要改成接收(rectangle Rectangle)。
go test# go-tdd-example/perimeter [go-tdd-example/perimeter.test]
./main_test.go:34:31: cannot use tt.args.Cycle (type Cycle) as type Rectangle in argument to Perimeter
FAIL go-tdd-example/perimeter [build failed]
可以看到在測試的過程中我們看到 Error ,這個 Error 是告訴我們
cannot use tt.args.Cycle (type Cycle) as type Rectangle in argument to Perimeter
這個意思大概是 Perimeter function 要求輸入一個 Rectangle 的類別,但是我們輸入 Cycle 類別。
go test# go-tdd-example/perimeter [go-tdd-example/perimeter.test]
./main_test.go:17:15: cannot use &Rectangle literal (type *Rectangle) as type Shape in field value:
*Rectangle does not implement Shape (missing Perimeter method)
./main_test.go:27:11: cannot use &Cycle literal (type *Cycle) as type Shape in field value:
*Cycle does not implement Shape (missing Perimeter method)
FAIL go-tdd-example/perimeter [build failed]
可以看到在測試的過程中我們看到 Error ,這個 Error 是告訴我們
./main_test.go:17:15: cannot use &Rectangle literal (type *Rectangle) as type Shape in field value:
*Rectangle does not implement Shape (missing Perimeter method)
./main_test.go:27:11: cannot use &Cycle literal (type *Cycle) as type Shape in field value:
*Cycle does not implement Shape (missing Perimeter method)
FAIL go-tdd-example/perimeter [build failed]
會看到這兩個錯誤,第一個錯誤是在說 *Rectangle 沒有實作 Shape interface ,因為他卻少了 Perimeter function 。
第二個錯是在說 *Cycle 沒有實作 Shape interface ,因為他卻少了 Perimeter function 。
補充main package
因為在測試的時候編譯器告訴我們 Rectangle 沒有實作 Shape interface ,因為他卻少了 Perimeter function ,那我們就識做一個吧,這時候我們需要用到 go 的function reciver 來幫 Rectangle 新增一個 Perimeter function。