英文:
Expected value is changed in Return Method of mockgen during its call
问题
我是你的中文翻译助手,以下是翻译好的内容:
我刚开始学习Go语言,最近尝试使用gomock包编写测试用例。我遇到了一个奇怪的问题。我正在尝试为GetUsers
编写测试用例,其实现如下:
func (ctrl *HttpController) GetUsers(w http.ResponseWriter, r *http.Request) {
users := ctrl.DS.GetUsers()
// 修改了用户的名称
users[0].Name = "User C"
utility.JsonOkResponse(w, users)
}
这是我的测试函数:
func TestGetUsers_Success(t *testing.T) {
// 设置模拟接口
ctrl, ds, _ := setup(t)
// 设置预期的函数调用
expected := mock_data.Users
ds.EXPECT().GetUsers().Return(expected)
// 创建请求
req := httptest.NewRequest("GET", "/users", nil)
// 生成路由器
mux := mux.NewRouter()
mux.HandleFunc("/users", ctrl.GetUsers)
// 创建响应记录器
resp := httptest.NewRecorder()
mux.ServeHTTP(resp, req)
// 检查响应内容
assert.Equal(t, http.StatusOK, resp.Code)
var users []model.User
json.Unmarshal(resp.Body.Bytes(), &users)
assert.Equal(t, expected, users)
}
这是mock_data.Users的定义:
var Users = []model.User{user1, user2}
// 示例用户数据
var user1 = model.User{
ID: 1,
Name: "User A",
Email: "usera@gmail.com",
}
var user2 = model.User{
ID: 2,
Name: "User B",
Email: "userb@gmail.com",
}
现在,我正在尝试不同的场景以便更好地理解。所以,在这种情况下,我的测试用例应该失败(尽管它仍然通过),因为我在GetUsers
函数中更改了一个用户的名称,但奇怪的是,测试用例仍然通过,并且当我执行assert.Equal(t, expected, users)
时,expected
中的值也被更改为新的用户名User C。是不是在mockgen的Return
方法中将expected值作为指针传递,还是我漏掉了一些基本的东西?
英文:
I am new to Go and recently, I am trying to write test cases using gomock package. I encountered a strange problem. I am trying to write test case for GetUsers
whose
implementation is
func (ctrl *HttpController) GetUsers(w http.ResponseWriter, r *http.Request) {
users := ctrl.DS.GetUsers()
// changed the name of the user
users[0].Name = "User C"
utility.JsonOkResponse(w, users)
}
This is my test function
func TestGetUsers_Success(t *testing.T) {
// setup mock interfaces
ctrl, ds, _ := setup(t)
// setup expected function calls
expected := mock_data.Users
ds.EXPECT().GetUsers().Return(expected)
// create the request
req := httptest.NewRequest("GET", "/users", nil)
// generate router
mux := mux.NewRouter()
mux.HandleFunc("/users", ctrl.GetUsers)
// create response recorder
resp := httptest.NewRecorder()
mux.ServeHTTP(resp, req)
// check response contents
assert.Equal(t, http.StatusOK, resp.Code)
var users []model.User
json.Unmarshal(resp.Body.Bytes(), &users)
assert.Equal(t, expected, users)
}
These are the mock_data.Users
var Users = []model.User{user1, user2}
// sample user data
var user1 = model.User{
ID: 1,
Name: "User A",
Email: "usera@gmail.com",
}
var user2 = model.User{
ID: 2,
Name: "User B",
Email: "userb@gmail.com",
}
Now, I was expermenting with different scenarios for better understanding. So, in this case my test case should fail (although it still passes) because I changed one of the user's name in GetUsers
function but strangely, the test case still passes and when I do assert.Equal(t, expected, users)
the value in the expected is also changed with new user name User C. Is the expected value passed as pointer in Return method of mockgen or Am I missing something basic ?
答案1
得分: 1
这是因为当你执行expected := mock_data.Users
时,你只是复制了切片头部,而没有复制底层数组,所以对第一个切片数组的更改会影响到另一个切片。请参考这篇文章获取更多详细信息。
如果你想复制切片,你需要使用内置的copy
函数,像这样:
expected := make([]model.User, len(mock_data.Users))
copy(expected, mock_data.Users)
英文:
This happens because when you do expected := mock_data.Users
you are making a copy of the slice header, but not the underlying array, so changes to the array of the first slice will change the other. Please take a look at this article for more details.
If you want to make a copy of the slice you need to use the builtin copy
function, like so:
expected := make([]model.User, len(mock_data.Users))
copy(expected, mock_data.Users)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论