Home > Enterprise >  Unit testing on imported package
Unit testing on imported package

Time:05-11

I am currently creating unit testing for a project, i need to test a function that have dependency injection without interface from an entity. Here is the code

func (u UserUsecase) CreateUser(ctx context.Context, p request.StoreUser) (entity.User, error) {
    user := &entity.User{
        Username:  p.Username,
        Email:     p.Email,
        Phone:     p.Phone,
        Password:  p.Password,
        CompanyID: p.CompanyID,
    }

    err := user.Create(ctx, u.userRepo)
    if err != nil {
        return entity.User{}, err
    }

    return *user, nil
}

In the user.Create(ctx, u.userRepo) there is a function that call another function so when I create a test function I've been stuck and getting invalid memory address or null pointer. Here is the code in the user.Create(ctx, u.userRepo)

func (u *User) Create(ctx context.Context, ur UserStoreRepo) error {
    u.UUID = uuid.New()
    u.Status = constanta.ACTIVE
    pass, err := helpers.Bcrypting(u.Password)
    if err != nil {
        return err
    }
    u.Password = pass

    err = ur.Store(ctx, u)
    return err
}

What is the best method to override the user.Create(ctx, u.userRepo) so i can test the CreateUser(ctx context.Context, p request.StoreUser).

CodePudding user response:

Use interface and use mock struct implements it to test

type User interface {
   CreateUser(ctx context.Context, p request.StoreUser) (entity.User, error)
}

type user struct {
//... user struct 
}

func (u *user) CreateUser(ctx context.Context, p request.StoreUser) (entity.User, error) {
//... real code goes here
}

Then create a mock

type mockUser struct {
 result entity.User
 err error
}
func (u *mockUser) CreateUser(ctx context.Context, p request.StoreUser) (entity.User, error) {
    return u.result, u.err
}

Then create a test like

func TestError(t *testing.T) {
    errMockUser := &mockUser{nil,erorrs.New("error")}
    result, err := errMockUser.CreateUser(context.Background(), 
    request.StoreUser{})
    assert.Nil(t, result)
    assert.Equal(t, errors.New("error"), err)
}
  • Related