lundi 2 décembre 2019

Go Test failing or passing depending on path supplied

I have a simple DAO with a test, which seems to pass if I pass the specific path to the domain package. However, if I run go test with the more generic find all.. path, then the test fails.

Can anyone shine any light as to why this is happening:

PASSES: go test github.com/repo/path/to/domain

FAILS: go test ./... - the domain package is the only package failing here.

The test is failing with the error undefined: GetUser

package domain

import (
    "net/http"
    "testing"

    "github.com/stretchr/testify/assert"
)

func TestGetUserNoUserFound(t *testing.T) {

    user, err := UserDao.GetUser(0)

    assert.Nil(t, user, "We were not expecting a use with id 0")
    assert.NotNil(t, err, "We were expecting an error with user id is 0")

}

The simple DAO:

package domain

import (
    "fmt"
    "log"
    "net/http"

    "github.com/repo/path/to/utils"
)

//mock
var (
    users = map[int64]*User{
        123: &User{ID: 1, FistName: "Name 1", LastName: "Name 2", Email: "someEmail@email.com"},
    }

    UserDao userDaoInterface
)

func init() {
    UserDao = &userDao{}
}

type userDao struct{}

type userDaoInterface interface {
    GetUser(int64) (*User, *utils.ApplicationError)
}

func (u *userDao) GetUser(userId int64) (*User, *utils.ApplicationError) {

    log.Println("We're accessing the database")

    if user := users[userId]; user != nil {
        return user, nil
    }

    return nil, &utils.ApplicationError{
        Message:    fmt.Sprintf("user %v was not found", userId),
        StatusCode: http.StatusNotFound,
        Code:       "not found",
    }
}

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire