4

您可以在以下位置找到我正在处理的代码:https ://github.com/bigblind/marvin

gometalinter从go vet给我以下错误:

accounts/interactors/accounts_test.go:16::error: 文字从 ma 复制锁定值:github.com/bigblind/marvin/accounts.MockAccountStore 包含 github.com/bigblind/marvin/vendor/github.com/stretchr/testify/ mock.Mock 包含 sync.Mutex(兽医)

如果您不熟悉 gometalinter,那没关系,它只是运行其他工具,包括 go vet。

因此,这是accounts/interactors/accounts_test.go:16上下文周围行的代码:

ma := accounts.NewMockAccountStore()
ma.On("SaveAccount", mock.AnythingOfType("Account")).Return(nil)
ca := CreateAccount{ma}

第 16 行是上面代码片段的最后一行。我们正在获取accounts.NewMockAccountStore() and putting it in the first field of a newCreateAccount` 结构的结果。

这是 MockAccountStore 的样子:

type MockAccountStore struct {
    *mock.Mock
}

func NewMockAccountStore() *MockAccountStore {
    mo := mock.Mock{}
    ma := MockAccountStore{&mo}
    return &ma
}

正如你在这里看到的,我经常使用指针。NewMockAccountStore返回指向 的指针MockAccountStore,并且MockAccountStore包含指向 a 的指针mock.Mock

所以当我把这个新创建的东西MockAccountStore放在我的CreateAccount结构中时,我希望它基本上只是将指针分配给那个字段,而不复制任何东西。

最后,让我们看一下CreateAccount结构:

type CreateAccount struct {
    AccountStore domain.AccountStore
}

所以CreateAccount只有一个字段用于存储domain.AccountStore. domain.AccountStore是一个接口,它MockAccountStore实现。

我能想出的唯一解释是分配给具有接口类型的字段以某种方式导致复制,但我不确定为什么,因为据我所知,接口值只是指向底层的指针类型。

谁能向我解释为什么去兽医抱怨?


看来我不是真的抄袭。

我已将代码更改如下:

ma := accounts.NewMockAccountStore()
ma.On("SaveAccount", mock.AnythingOfType("Account")).Return(nil)
s := fmt.Sprintf("Initial %#v\n", ma)
ca := CreateAccount{ma}
panic(s + fmt.Sprintf("assigned: %#v", ca.AccountStore))

当包含此代码的测试运行时,我收到以下恐慌消息:

初始 &accounts.MockAccountStore{Mock:(*mock.Mock)(0xc420019780)} 分配:&accounts.MockAccountStore{Mock:(*mock.Mock)(0xc420019780)}

如您所见,两个值都指向同一个mock.Mock指针(0xc420019780)。

那我该怎么说go vet呢?


应@putu 的要求,以下是命令输出accounts/interactors

vet: accounts/interactors/accounts_test.go:8:2: could not import github.com/stretchr/testify/require (can't find import: github.com/bigblind/marvin/vendor/github.com/stretchr/testify/require)
Checking file accounts/interactors/accounts.go
Checking file accounts/interactors/login.go
Checking file accounts/interactors/accounts_test.go
accounts/interactors/accounts_test.go:15: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/accounts_test.go:28: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/accounts_test.go:35: github.com/bigblind/marvin/accounts/domain.Account composite literal uses unkeyed fields
accounts/interactors/accounts_test.go:40: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
Checking file accounts/interactors/login_test.go
accounts/interactors/login_test.go:22: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:36: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:47: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:58: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:70: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
accounts/interactors/login_test.go:80: literal copies lock value from ma: github.com/bigblind/marvin/accounts.MockAccountStore contains github.com/bigblind/marvin/vendor/github.com/stretchr/testify/mock.Mock contains sync.Mutex
4

0 回答 0