由于特定库中的依赖项错误,导致测试失败

时间:2020-11-10 00:32:17

标签: go

我正在尝试进行一些go测试,错误如下:

▶ go test -timeout 100000000s -cover -race
# github.com/tj/assert
../../../../../go/pkg/mod/github.com/tj/assert@v0.0.1/require.go:191:29: too many arguments in call to assert.HTTPBodyContains
    have (TestingT, http.HandlerFunc, string, string, url.Values, io.Reader, interface {}, []interface {}...)
    want (assert.TestingT, http.HandlerFunc, string, string, url.Values, interface {}, ...interface {})
../../../../../go/pkg/mod/github.com/tj/assert@v0.0.1/require.go:203:32: too many arguments in call to assert.HTTPBodyNotContains
    have (TestingT, http.HandlerFunc, string, string, url.Values, io.Reader, interface {}, []interface {}...)
    want (assert.TestingT, http.HandlerFunc, string, string, url.Values, interface {}, ...interface {})
FAIL    github.com/myrepo/myproject/lib/somelib [build failed]

在我的go.mod

▶ cat go.mod | grep -i tj
    github.com/tj/assert v0.0.1

是什么原因导致上述错误?

1 个答案:

答案 0 :(得分:3)

github.com/tj/assert@v0.0.1的第191行是:

if !assert.HTTPBodyContains(t, handler, method, url, values, body, str, msgAndArgs...)

由于导入assert "github.com/stretchr/testify/assert",因此引用了stretchr testify package,该定义HTTPBodyContains

 HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{})

因此您得到的错误原因似乎很清楚;错误状态:

have (TestingT, http.HandlerFunc, string, string, url.Values, io.Reader, interface {}, []interface {}...)
want (assert.TestingT, http.HandlerFunc, string, string, url.Values, interface {}, ...interface {})

我怀疑真正的问题是:

  • 您打算运行HTTPBodyContains的本地版本时,导入assert "github.com/stretchr/testify/assert"并在该程序包中调用函数。要解决此问题,请从函数名称之前删除assert.
  • 您正在使用assert@v0.0.1,但具有更高版本。使用go get检索最新版本(请参阅upgrading dependencies上的文档)。