Skip to content

Latest commit

 

History

History
51 lines (35 loc) · 1.36 KB

unit_test.md

File metadata and controls

51 lines (35 loc) · 1.36 KB

Unit Test

To testing module that generated by Bima Cli is easy, for example we want to test GetPaginated() method in todos module from ReadMe

Create Test File

Create module_test.go in folder todos

Writing Test Scenario

We use Mockery to generate mock object, i hope you are familiar or try to use this tool after read this doc. And the codes to test GetPaginated() function are easy and simple as you see below.

package todos

import (
	grpcs "app/protos/builds"
	"context"
	"testing"

	"github.com/bimalabs/framework/v4/mocks"
	handler "github.com/bimalabs/framework/v4/mocks/handlers"
	"github.com/bimalabs/framework/v4/paginations"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
)

func Test_Pagination(t *testing.T) {
	bhandler := handler.NewHandler(t)
	bhandler.On("Paginate", mock.Anything, mock.Anything).Return(paginations.Metadata{}).Maybe()

	bmodule := mocks.NewModule(t)
	bmodule.On("Paginator").Return(&paginations.Pagination{}).Maybe()
	bmodule.On("Handler").Return(bhandler).Maybe()

	module := Module{
		Module: bmodule,
		Model:  &Todo{},
	}

	response, err := module.GetPaginated(context.TODO(), &grpcs.PaginationRequest{})

	assert.Nil(t, err)
	assert.NotNil(t, response)

    bhandler.AssertExpectations(t)
	bmodule.AssertExpectations(t)
}