Unit testing 将testing.T用作匿名结构字段:“0”;调用此.T.common.Fail的参数过多”;

Unit testing 将testing.T用作匿名结构字段:“0”;调用此.T.common.Fail的参数过多”;,unit-testing,struct,go,Unit Testing,Struct,Go,我试图解决围棋中的kata问题,作为一个练习,我的测试用例中出现了以下编译器错误: 调用this.T.common.Fail时参数太多 我使用其他方法将testing.T包装到一个结构中,作为一个匿名结构字段: package main import ( "fmt" "testing" ) type assertions struct { *testing.T } func (this assertions) assert_equal(expected int, ac

我试图解决围棋中的kata问题,作为一个练习,我的测试用例中出现了以下编译器错误:

调用this.T.common.Fail时参数太多

我使用其他方法将
testing.T
包装到一个结构中,作为一个匿名结构字段:

package main

import (
    "fmt"
    "testing"
)

type assertions struct {
    *testing.T
}
func (this assertions) assert_equal(expected int, actual int) {
    if (expected != actual) {
        this.Fail(fmt.Sprintf("Failed asserting that %v is %v", actual, expected));
    }
}

func TestChop(t *testing.T) {
  test := assertions{t}

  test.assert_equal(-1, Chop(3, []int{}))
  test.assert_equal(-1, Chop(3, []int{1}))
  ...
}
我希望
this.Fail
在匿名
testing.T
struct字段上调用
Fail()
,该字段采用字符串参数。为什么不是这样?这个.t.common.Fail来自哪里?我在
测试
软件包文档中找不到任何有关
通用
的参考

Fail将函数标记为已失败但仍继续执行

T.common.Fail()
没有参数

尝试
Errorf

Errorf相当于Logf后跟Fail

比如说,

this.Errorf("Failed asserting that %v is %v", actual, expected)

谢谢这就解释了我愚蠢的错误和
T.common
的起源。当然,
Errorf
是我想要的方法。
func (c *T) Fail()
func (c *T) Errorf(format string, args ...interface{})
this.Errorf("Failed asserting that %v is %v", actual, expected)