質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Q&A

解決済

1回答

1536閲覧

GoのTypeAssertionが分からない

kuntao

総合スコア16

Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

0グッド

0クリップ

投稿2018/09/24 06:57

編集2018/09/26 07:21

概要

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

スタックトレースの中で出てくる行へのリンク

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

stretchr/testify のバグっぽいですね。
このPullRequestがマージされれば直りそうです。

  • 確認方法

検証のためにdep(バージョン管理ツール)でテスト用ディレクトリ配下で、上記のPRのとおりtestifyのライブリを直してみました。

$ cd *your project dir* $ ls main.go main_test.go vendor/ $ vim ./vendor/github.com/stretchr/testify/assert/assertions.go :1352 var e, a string if ek != reflect.String { e = spewConfig.Sdump(expected) a = spewConfig.Sdump(actual) } else { e = reflect.ValueOf(expected).String() a = reflect.ValueOf(actual).String() // e = expected.(string) // a = actual.(string) } $ go test --- FAIL: TestMain (0.00s) main_test.go:22: Error Trace: main_test.go:22 Error: Not equal: expected: "expected string" actual : "actual stirng" Diff: --- Expected +++ Actual @@ -1 +1 @@ -expected string +actual stirng Test: TestMain FAIL exit status 1
  • 補足:dep導入は下記のとおりすればできます
$ go get -u github.com/golang/dep/cmd/dep $ cd *your project dir* $ ls main.go main_test.go $ dep init $ ls Gopkg.lock Gopkg.toml main.go main_test.go vendor/ $ go test ... ~~ /vendor/github.com/stretchr/testify/assert/assertions.go:1352 +0x101 ...
  • 型アサーションについて

こちらがわかりやすいかと: https://qiita.com/atsaki/items/3554f5a0609c59a3e10d#型アサーション
上記PRの確認はこちらでもできますね: https://play.golang.org/p/Oi7Q3mMJee2

投稿2018/09/29 15:47

編集2018/09/29 16:03
mats0228

総合スコア219

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kuntao

2018/09/30 00:35

testifyの方のバグだったんですね。パッケージ側のコードは合っているはずだというつもりで読んでいたのでどうやって書けばいいか分からず途方にくれているところでした。。mergeを待つなりforkするなりして対応します。 分かりやすいリンクまでありがとうございます。ゴリゴリと実装を進めていきたいと思います。 ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問