programming

How to Create Unit Tests Using the moq Library in Golang

Carrying out unit tests using this mocking method is usually used if several functions have been carried out in interface format so that we can assume that if we call the interface function we believe that it should produce the correct program.

Currently to create mocking we use the library from github.com/matryer/moq. If there is no such library, we can download it first with the command below.

$ go install github.com/matryer/moq@latest

Below we will try to create a project that has the interface function below.

package main

import "fmt"

type User struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

//go:generate moq -out main_mock_test.go . UserRepositoryInterface
type UserRepositoryInterface interface {
	GetAllUsers() ([]User, error)
}

type UserService struct {
	UserRepositoryInterface
}

func (s UserService) GetUser() ([]User, error) {
	users, _ := s.UserRepositoryInterface.GetAllUsers()
	for i := range users {
		users[i].Password = "*****"
	}
	return users, nil
}

type UserRepository struct{}

func (r UserRepository) GetAllUsers() ([]User, error) {
	users := []User{
		{"real", "real"},
		{"real2", "real2"},
	}
	return users, nil
}

func main() {
	repository := UserRepository{}
	service := UserService{repository}
	users, _ := service.GetUser()
	fmt.Println(users)
}

Then continue with how to create a unit test which can be seen below

package main

import (
	"fmt"
	"reflect"
	"testing"

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

type UserRepositoryMock struct {
	mock.Mock
}

func (r UserRepositoryMock) GetAllUsers() ([]User, error) {
	args := r.Called()
	users := []User{
		{"mock", "*****"},
	}
	return users, args.Error(1)
}

func TestService_GetUser(t *testing.T) {
	repository := UserRepositoryMock{}
	repository.On("GetAllUsers").Return([]User{}, nil)

	service := UserService{repository}
	users, _ := service.GetUser()
	for i := range users {
		assert.Equal(t, users[i].Password, "*****", "user password must be encrypted")
	}
	fmt.Println(users)
}

func TestUserService_GetUser(t *testing.T) {
	type fields struct {
		UserRepositoryInterface UserRepositoryInterface
	}
	tests := []struct {
		name    string
		fields  fields
		want    []User
		wantErr bool
	}{
		{
			name: "case ambil data user",
			fields: fields{
				UserRepositoryInterface: UserRepositoryMock{},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s := UserService{
				UserRepositoryInterface: tt.fields.UserRepositoryInterface,
			}
			got, err := s.GetUser()
			if (err != nil) != tt.wantErr {
				t.Errorf("UserService.GetUser() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("UserService.GetUser() = %v, want %v", got, tt.want)
			}
		})
	}
}
comments powered by Disqus