英文:
Golang using Null*s in tests
问题
我有一些Go的测试,我正在尝试正确使用Null*s,但是go test报错了,我不确定哪里出错了。
这是错误信息:
go test ./...
utils/db.go:1:1: 期望 'package',找到 'EOF'
? mobifit [没有测试文件]
# mobifit/app/entities
app/entities/user_test.go:34: 无法将dateOfBirth(类型为func() time.Time)分配给类型time.Time
app/entities/user_test.go:58: User.Gender未定义(User类型没有Gender方法)
app/entities/user_test.go:59: User.DateOfBirth未定义(User类型没有DateOfBirth方法)
app/entities/user_test.go:60: User.Height未定义(User类型没有Height方法)
app/entities/user_test.go:61: User.CurrentWeight未定义(User类型没有CurrentWeight方法)
app/entities/user_test.go:65: User.Email未定义(User类型没有Email方法)
app/entities/user_test.go:66: User.HashedPassword未定义(User类型没有HashedPassword方法)
app/entities/user_test.go:67: User.FirstName未定义(User类型没有FirstName方法)
app/entities/user_test.go:68: User.LastName未定义(User类型没有LastName方法)
app/entities/user_test.go:69: User.CreatedAt未定义(User类型没有CreatedAt方法)
app/entities/user_test.go:69: 错误太多
FAIL mobifit/app/entities [构建失败]
? mobifit/app/handlers [没有测试文件]
以下是测试代码:
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: 应该检查是否非空
}
更新:User类型在哪里?
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() {
}
英文:
I have some Go tests, and I am trying to use Null*s correctly, but go test is giving errors and I am not sure what is wrong.
Here is the error message:
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]
Here are the tests:
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
}
UPDATE: Where is the User type?
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() {
}
答案1
得分: 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
值的代码。
英文:
For this error:
app/entities/user_test.go:34: cannot use dateOfBirth (type func() time.Time) as type time.Time in assignment
You seem to be trying to assign the function time.Now
to a time.Time
field. Presumably you want to call the function?
For these errors:
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)
...
It looks like you're trying to access fields on the type User
, when you really want to be checking the variable user
(note the case difference).
Lastly, if you are creating sample data with sql.Null*
fields and want it to resemble real data, you should probably set the Valid
field to true
so that you don't trip up code that is actually checking for NULL
values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论