概要
goのテストを stretchr/testify を使って書こうとしています。そこで以下に乗せるようなコードを書いているのですが、TypeAssertionに失敗しpanicを起こしてしまいます。
Goを書くのが初めてでTypeAssertionがよく分かっていない状態です。どのようにすれば動くor元々そうするべきないなど意見をいただければと思います。
よろしくおねがいします。
(一応 こちらのFAQ を読んでgoのassertionに対する考え方は把握しています。)
詳細
環境
macOS High Sierra 10.13.6
Go 1.11
stretchr/testify v1.2.2
サンプルコード
main.go
Go
1package main 2 3import "fmt" 4 5type MyString string 6 7func main() { 8 fmt.Println("Nothing Implemented") 9}
main_test.go
Go
1package main 2 3import ( 4 "testing" 5 "github.com/stretchr/testify/assert" 6) 7 8func TestMain(t *testing.T) { 9 var expected, actual MyString 10 11 expected = "expected string" 12 actual = "actual stirng" 13 14 // このassertで落ちる 15 assert.Equal(t, expected, actual, "string doesn't match") 16}
起きるエラー
PlainText
1% go test 2--- FAIL: TestMain (0.00s) 3panic: interface conversion: interface {} is main.MyString, not string [recovered] 4 panic: interface conversion: interface {} is main.MyString, not string 5 6goroutine 19 [running]: 7testing.tRunner.func1(0xc000124100) 8 /Users/kuntao/.gvm/gos/go1.11/src/testing/testing.go:792 +0x387 9panic(0x1299640, 0xc00009cfc0) 10 /Users/kuntao/.gvm/gos/go1.11/src/runtime/panic.go:513 +0x1b9 11github.com/stretchr/testify/assert.diff(0x1280900, 0xc000098e50, 0x1280900, 0xc000098e60, 0x0, 0x0) 12 /Users/kuntao/.gvm/pkgsets/go1.11/global/src/github.com/stretchr/testify/assert/assertions.go:1352 +0x454 13github.com/stretchr/testify/assert.Equal(0x1330540, 0xc000124100, 0x1280900, 0xc000098e50, 0x1280900, 0xc000098e60, 0xc0000c7f88, 0x1, 0x1, 0x6fe13330a2d8) 14 /Users/kuntao/.gvm/pkgsets/go1.11/global/src/github.com/stretchr/testify/assert/assertions.go:338 +0x26c 15try_testify.TestMain(0xc000124100) 16 /Users/kuntao/.gvm/pkgsets/go1.11/global/src/try_testify/main_test.go:16 +0x12e 17testing.tRunner(0xc000124100, 0x12fc718) 18 /Users/kuntao/.gvm/gos/go1.11/src/testing/testing.go:827 +0xbf 19created by testing.(*T).Run 20 /Users/kuntao/.gvm/gos/go1.11/src/testing/testing.go:878 +0x353 21exit status 2 22FAIL try_testify 0.039s
スタックトレースの中で出てくる行へのリンク
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/30 00:35