我有一些Go测试,我正在尝试正确使用Null *,但是测试是错误的,我不确定是什么问题。
以下是错误消息:
go test ./...
utils/db.go:1:1: expected 'package', found 'EOF'
? mobifit [no test files]
# mobifit/app/entities
app/entities/user_test.go:34: cannot use dateOfBirth (type func() time.Time) as type time.Time in assignment
app/entities/user_test.go:58: User.Gender undefined (type User has no method Gender)
app/entities/user_test.go:59: User.DateOfBirth undefined (type User has no method DateOfBirth)
app/entities/user_test.go:60: User.Height undefined (type User has no method Height)
app/entities/user_test.go:61: User.CurrentWeight undefined (type User has no method CurrentWeight)
app/entities/user_test.go:65: User.Email undefined (type User has no method Email)
app/entities/user_test.go:66: User.HashedPassword undefined (type User has no method HashedPassword)
app/entities/user_test.go:67: User.FirstName undefined (type User has no method FirstName)
app/entities/user_test.go:68: User.LastName undefined (type User has no method LastName)
app/entities/user_test.go:69: User.CreatedAt undefined (type User has no method CreatedAt)
app/entities/user_test.go:69: too many errors
FAIL mobifit/app/entities [build failed]
? mobifit/app/handlers [no test files]
以下是测试:
package entities
import (
// "database/sql"
// "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var (
email = "leebrooks0@gmail.com"
hashedPassword = "password"
firstName = "Lee"
lastName = "Brooks"
gender = Male
dateOfBirth = time.Now
height = 1.85
currentWeight = 101.3
)
func privacyConcernedUser() *User {
user := new(User)
user.Email.String = email
user.HashedPassword.String = hashedPassword
user.FirstName.String = firstName
user.LastName.String = lastName
return user
}
func normalUser() *User {
user := privacyConcernedUser()
user.Gender.String = gender
user.DateOfBirth.Time = dateOfBirth
user.Height.Float64 = height
user.CurrentWeight.Float64 = currentWeight
return user
}
func TestSignUpPrivacyConcernedUser(t *testing.T) {
user := privacyConcernedUser()
user.SignUp()
checkMandatoryValues(t, user)
assert.Nil(t, user.Gender.String)
assert.Nil(t, user.DateOfBirth.Time)
assert.Nil(t, user.Height.Float64)
assert.Nil(t, user.CurrentWeight.Float64)
}
func TestSignUpNormalUser(t *testing.T) {
user := normalUser()
user.SignUp()
checkMandatoryValues(t, user)
assert.Equal(t, User.Gender.String, gender)
assert.Equal(t, User.DateOfBirth.Time, dateOfBirth)
assert.Equal(t, User.Height.Float64, height)
assert.Equal(t, User.CurrentWeight.Float64, currentWeight)
}
func checkMandatoryValues(t *testing.T, user *User) {
assert.Equal(t, User.Email.String, email)
assert.Equal(t, User.HashedPassword.String, hashedPassword)
assert.Equal(t, User.FirstName.String, firstName)
assert.Equal(t, User.LastName.String, lastName)
assert.NotNil(t, User.CreatedAt.Time) // TODO: Should rather check for not empty
}
更新:用户类型在哪里?
type User struct {
Id sql.NullInt64
Email sql.NullString
HashedPassword sql.NullString
RoleId sql.NullInt64
FirstName sql.NullString
LastName sql.NullString
Gender sql.NullString
DateOfBirth mysql.NullTime
Height sql.NullFloat64
CurrentWeight sql.NullFloat64
CreatedAt mysql.NullTime
ConfirmedAt mysql.NullTime
LastActivityAt mysql.NullTime
DeletedAt mysql.NullTime
}
func (u *User) SignUp() {
}
答案 0 :(得分:3)
对于此错误:
app/entities/user_test.go:34: cannot use dateOfBirth (type func() time.Time) as type time.Time in assignment
您似乎正在尝试将函数time.Now
分配给time.Time
字段。想必你要调用这个函数吗?
对于这些错误:
app/entities/user_test.go:58: User.Gender undefined (type User has no method Gender)
app/entities/user_test.go:59: User.DateOfBirth undefined (type User has no method DateOfBirth)
...
当你真的想要检查变量User
时,看起来你正试图访问user
类型的字段(注意案例差异)。
最后,如果您要使用sql.Null*
字段创建示例数据并希望它类似于实际数据,则应该将Valid
字段设置为true
,这样您就不会绊倒实际检查NULL
值的代码。