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.

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号