提取修改猪群数量逻辑
This commit is contained in:
@@ -25,7 +25,7 @@ type MockUserRepository struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Create 模拟 UserRepository 的 Create 方法
|
||||
// CreateTx 模拟 UserRepository 的 CreateTx 方法
|
||||
func (m *MockUserRepository) Create(user *models.User) error {
|
||||
args := m.Called(user)
|
||||
return args.Error(0)
|
||||
@@ -90,8 +90,8 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "password123",
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 模拟 Create 成功
|
||||
m.On("Create", mock.AnythingOfType("*models.User")).Return(nil).Run(func(args mock.Arguments) {
|
||||
// 模拟 CreateTx 成功
|
||||
m.On("CreateTx", mock.AnythingOfType("*models.User")).Return(nil).Run(func(args mock.Arguments) {
|
||||
// 模拟数据库自动填充 ID
|
||||
userArg := args.Get(0).(*models.User)
|
||||
userArg.ID = 1 // 设置一个非零的 ID
|
||||
@@ -114,7 +114,7 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "123", // 密码少于6位
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 不会调用 Create 或 FindByUsername
|
||||
// 不会调用 CreateTx 或 FindByUsername
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(controller.CodeBadRequest),
|
||||
@@ -128,7 +128,7 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "password123",
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 不会调用 Create 或 FindByUsername
|
||||
// 不会调用 CreateTx 或 FindByUsername
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(controller.CodeBadRequest),
|
||||
@@ -143,8 +143,8 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "password123",
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 模拟 Create 失败,因为用户名已存在
|
||||
m.On("Create", mock.AnythingOfType("*models.User")).Return(errors.New("duplicate entry")).Once()
|
||||
// 模拟 CreateTx 失败,因为用户名已存在
|
||||
m.On("CreateTx", mock.AnythingOfType("*models.User")).Return(errors.New("duplicate entry")).Once()
|
||||
// 模拟 FindByUsername 找到用户,确认是用户名重复
|
||||
m.On("FindByUsername", "existinguser").Return(&models.User{Username: "existinguser"}, nil).Once()
|
||||
},
|
||||
@@ -161,8 +161,8 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "password123",
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 模拟 Create 失败,通用数据库错误
|
||||
m.On("Create", mock.AnythingOfType("*models.User")).Return(errors.New("database error")).Once()
|
||||
// 模拟 CreateTx 失败,通用数据库错误
|
||||
m.On("CreateTx", mock.AnythingOfType("*models.User")).Return(errors.New("database error")).Once()
|
||||
// 模拟 FindByUsername 找不到用户,确认不是用户名重复
|
||||
m.On("FindByUsername", "db_error_user").Return(nil, gorm.ErrRecordNotFound).Once()
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user