php小编小新在这篇文章中将为您介绍在Go语言中编写单元测试时出现的一种常见错误,即断言错误。当我们在编写单元测试时,有时会遇到无法确定返回值的情况,这会导致意外的方法调用错误。在本文中,我们将讨论这个问题的原因和解决方法,帮助您更好地处理断言错误,确保单元测试的准确性和可靠性。
问题内容
我在 go 中使用 testify
为我的服务方法编写单元测试,除了更新方法之外,所有方法都工作正常,因为在更新方法中我在更新方法中调用同一服务的另一个方法(“getbyid”) .
在我的服务中实现 update 方法:
func (ts *teamservice) update(team *team.team) apperror {
t, err := ts.teamrepo.getbyid(team.id)
if err != nil {
return err
}
if t.teamownerid != team.teamownerid {
return newforbiddenerror(forbiddenerr)
}
return ts.teamrepo.update(team)
}
mockrepo 更新方法:
func (t *teamrepomock) update(team *team.team) apperror {
args := t.called(team)
if args.error(0) != nil {
return newnotfounderror(args.error(0))
}
return nil
}
测试的实施:
func testupdate(t *testing.t) {
_, teamidgen, playeridgen := setupconfig()
t.run("update a team", func(t *testing.t) {
teamrepo, _, ts := setupteamservice(teamidgen, playeridgen)
teamrepo.on("update", testteam1).return(nil)
result := ts.update(testteam1)
assert.nil(t, result)
})
t.run("update a team fails", func(t *testing.t) {
teamrepo, _, ts := setupteamservice(teamidgen, playeridgen)
expected := oopserr
teamrepo.on("update", testteam1).return(expected)
result := ts.update(testteam1)
assert.equalvalues(t, expected.error(), result.error())
})
}
现在,当我运行测试时,出现以下错误:
--- FAIL: TestUpdate (0.01s)
--- FAIL: TestUpdate/Update_a_team (0.01s)
panic:
assert: mock: I don't know what to return because the method call was unexpected.
Either do Mock.On("GetByID").Return(...) first, or remove the GetByID() call.
This method was unexpected:
GetByID(string)
0: ""
at: [/home/waleem/Desktop/project/eazykhel_server/services/teamservice/team_service_init_test.go:18 /home/waleem/Desktop/project/eazykhel_server/services/teamservice/team_service.go:146 /home/waleem/Desktop/project/eazykhel_server/services/teamservice/team_service_test.go:277] [recovered]
panic:
我尝试在测试函数实现中调用 .on("update")
之前和之后调用 mock.on("getbyid")
,但它不起作用,而且我还修改了mockrepo update函数,但它没用。
解决方法
让我尝试帮助您解决问题。我通过一些简化复制了该存储库,只是为了发布相关代码。如果我在您的解决方案中没有错,有一个服务(teamservice
)调用底层包(teamrepo
)提供的一些方法。您想测试 teamservice
结构的 update
方法。回顾之后,让我先展示代码,然后我将尝试解释每个文件:
repo/repo.go
package repo
type team struct {
id int
teamownerid int
name string
}
type teamrepo struct{}
func (t *teamrepo) getbyid(id int) (team, error) {
return team{id: id, teamownerid: id, name: "myteam"}, nil
}
func (t *teamrepo) update(team team) error {
return nil
}
在此文件中,我们可以找到要模拟的方法。方法是:getbyid
和 update
。显然,这不是您的实际代码,但现在并不重要。
services/service.go
package services
import (
"errors"
"testifymock/repo"
)
type teamservice struct {
tr teamrepointerface
}
func newteamservice(repo teamrepointerface) *teamservice {
return &teamservice{
tr: repo,
}
}
type teamrepointerface interface {
getbyid(id int) (repo.team, error)
update(team repo.team) error
}
func (ts *teamservice) update(team *repo.team) error {
t, err := ts.tr.getbyid(team.id)
if err != nil {
return err
}
if t.teamownerid != team.teamownerid {
return errors.new("forbidden")
}
return ts.tr.update(*team)
}
在这里,我们可以在测试代码中看到将成为我们的被测系统的服务 (sut
)。通过依赖注入,我们将利用通过接口 teamrepointerface
注入的 repo
包提供的功能。
services/service_test.go
package services
import (
"errors"
"testing"
"testifymock/repo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// 1. declare the mock struct
type teamRepoMock struct {
mock.Mock
}
// 2. implement the interface
func (m *teamRepoMock) GetByID(id int) (repo.Team, error) {
args := m.Called(id)
return args.Get(0).(repo.Team), args.Error(1)
}
func (m *teamRepoMock) Update(team repo.Team) error {
args := m.Called(team)
return args.Error(0)
}
func TestUpdate(t *testing.T) {
t.Run("GoodUpdate", func(t *testing.T) {
// 3. instantiate/setup mock
repoMock := new(teamRepoMock)
repoMock.On("GetByID", 1).Return(repo.Team{ID: 1, TeamOwnerID: 1, Name: "test"}, nil).Times(1)
repoMock.On("Update", repo.Team{ID: 1, TeamOwnerID: 1, Name: "test"}).Return(nil).Times(1)
sut := NewTeamService(repoMock)
err := sut.Update(&repo.Team{ID: 1, TeamOwnerID: 1, Name: "test"})
// 4. check that all expectations were met on the mock
assert.Nil(t, err)
assert.True(t, repoMock.AssertExpectations(t))
})
t.Run("BadUpdate", func(t *testing.T) {
// 3. instantiate/setup mock
repoMock := new(teamRepoMock)
repoMock.On("GetByID", 1).Return(repo.Team{ID: 1, TeamOwnerID: 1, Name: "test"}, nil).Times(1)
repoMock.On("Update", repo.Team{ID: 1, TeamOwnerID: 1, Name: "test"}).Return(errors.New("some error while updating")).Times(1)
sut := NewTeamService(repoMock)
err := sut.Update(&repo.Team{ID: 1, TeamOwnerID: 1, Name: "test"})
// 4. check that all expectations were met on the mock
assert.Equal(t, "some error while updating", err.Error())
assert.True(t, repoMock.AssertExpectations(t))
})
}
在代码中,您可以找到一些注释以更好地详细说明所发生的情况。正如您所猜测的,问题是代码中缺少此调用:
repomock.on("getbyid", 1).return(repo.team{id: 1, teamownerid: 1, 姓名: "test"}, nil).times(1)
如果您运行我的解决方案,它也应该适合您。
如果这解决了您的问题或者还有任何其他问题,请告诉我!
以上就是断言:模拟:我不知道要返回什么,因为方法调用是意外的 在 Go 中编写单元测试时出错的详细内容,更多请关注编程网其它相关文章!