How to test Go Mock Repository inside loop on usecase

3.5k views Asked by At

I have a problem with testing with Repository Mock Database using testify.

I want to test a service/use case that creates a record database for each iteration. Here is the code:

This code contain mock of the database

mockrepository.go

package service

import(
  "errors"
  "github.com/stretchr/testify/mock"
)
type TestRepositoryMock struct {
    Mock mock.Mock
}

func (repository *TestRepositoryMock) CreateTodo(todo *Todo) error {
    arguments := repository.Mock.Called(todo)
    if arguments.Get(0) == nil {
        return errors.New("Error")
    } else {
        return nil
    }
}

func (repository *TestRepositoryMock) CreateTodoDetail(todo *TodoDetail) error {
    arguments := repository.Mock.Called(todo)
    if arguments.Get(0) == nil {
        return errors.New("Error")
    } else if arguments.Get(1) == nil {
        return errors.New("Error")
    }  else {
        return nil
    }
}

the logic inside this use case is to save TodoDetail for each number of items (NumberOfItems variable)

usecase.go

package service

import(
  "strconv"
)

type UseCase interface {
  SaveTodo(numberOfItems int) (string, error)
}

func NewUseCase(repo Repository) UseCase {
  return &useCase{repo: repo}
}

type useCase struct {
  repo Repository
}

func (uc *useCase) SaveTodo(numberOfItems int) (string, error){
  todo := Todo{
    Title: "this is title",
  }
  uc.repo.CreateTodo(&todo)

  for i := 0; i < numberOfItems; i++ {
    todoDetail := TodoDetail{
      ID: todo.ID,
      Item: "item "+ strconv.Itoa(i),
    }
    uc.repo.CreateTodoDetail(&todoDetail)
  }

  return "success", nil 
}
package service

import(
  "testing"
  "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/mock"
)

var repository = &TestRepositoryMock{Mock: mock.Mock{}}
var testService = useCase{repo: repository}

func TestService_SaveTodoSuccess(t *testing.T) {
  todo := Todo{
    Title: "this is title",
  }
  todoDetail := TodoDetail{
      ID: todo.ID,
      Item: "item 1",
    }
  repository.Mock.On("CreateTodo", &todo).Return(nil)
  repository.Mock.On("CreateTodoDetail", &todoDetail).Return(nil).Once()
  result, err := testService.SaveTodo(3)
  assert.Nil(t, err)
    assert.NotNil(t, result)
}

How to test the multiple call for CreateTodoDetail inside for loop?

2

There are 2 answers

0
Christian Mahardhika On BEST ANSWER

-SOLVED-

For testing on the loop use case, we should provide numbers of mock data. same as loop iteration.

Example. If use case has 3 iteration loop that calls the repository three times then we must provide 3 data mock.

the code should be like this:

func TestService_SaveTodoSuccess(t *testing.T) {
  // Mock Entity Todo
  todo := Todo{
    Title: "this is title",
  }

  // Mock Entity TodoDetail
  todoDetail1 := TodoDetail{
      ID: todo.ID,
      Item: "item 0",
    }
  todoDetail2 := TodoDetail{
      ID: todo.ID,
      Item: "item 1",
    }
  todoDetail3 := TodoDetail{
      ID: todo.ID,
      Item: "item 2",
    }
  repository.Mock.On("CreateTodo", &todo).Return(todo)
// calls 3 times "CreateTodoDetail" Repository
  repository.Mock.On("CreateTodoDetail", &todoDetail1).Return(todoDetail1)
  repository.Mock.On("CreateTodoDetail", &todoDetail2).Return(todoDetail2)
  repository.Mock.On("CreateTodoDetail", &todoDetail3).Return(todoDetail3)
  result, err := testService.SaveTodo(3)
  assert.Nil(t, err)
    assert.NotNil(t, result)
}

The scenario builds 3 different data on todoDetail to mock 3 times iterations for CreateTodoDetail repository

0
pgzmnk k On

To run the same test over a range of mocked responses, Mock has a .Once() method that enables repeating a single mock with each result needed.

// array to loop through
todoDetailArray := []TodoDetail{todoDetail1, todoDetail2}

// loop through mocks for each test scenario
for eachTodoDetail := range todoDetailArray {
    repository.Mock.On("CreateTodoDetail", eachTodoDetail).Return(eachTodoDetail).Once()
    ...
}