drf65218 2014-01-17 10:05
浏览 32
已采纳

Golang在测试中使用Null * s

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条回答 默认 最新

  • dsft8327 2014-01-17 11:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路