title: How to test Go Mock Repository inside loop on usecase
body: <p>I have a problem with testing with Repository Mock Database using testify.</p>
  <p>I want to test a service/use case that creates a record database for each iteration. Here is the code:</p>
  <p>This code contain mock of the database</p>
  <p><em><strong>mockrepository.go</strong></em></p>
  <pre class="lang-golang prettyprint-override"><code>package service
  
  import(
    &quot;errors&quot;
    &quot;github.com/stretchr/testify/mock&quot;
  )
  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(&quot;Error&quot;)
      } else {
          return nil
      }
  }
  
  func (repository *TestRepositoryMock) CreateTodoDetail(todo *TodoDetail) error {
      arguments := repository.Mock.Called(todo)
      if arguments.Get(0) == nil {
          return errors.New(&quot;Error&quot;)
      } else if arguments.Get(1) == nil {
          return errors.New(&quot;Error&quot;)
      }  else {
          return nil
      }
  }
  </code></pre>
  <p>the logic inside this use case is to save TodoDetail for each number of items <code>(NumberOfItems variable)</code></p>
  <p><em><strong>usecase.go</strong></em></p>
  <pre class="lang-golang prettyprint-override"><code>package service
  
  import(
    &quot;strconv&quot;
  )
  
  type UseCase interface {
    SaveTodo(numberOfItems int) (string, error)
  }
  
  func NewUseCase(repo Repository) UseCase {
    return &amp;useCase{repo: repo}
  }
  
  type useCase struct {
    repo Repository
  }
  
  func (uc *useCase) SaveTodo(numberOfItems int) (string, error){
    todo := Todo{
      Title: &quot;this is title&quot;,
    }
    uc.repo.CreateTodo(&amp;todo)
  
    for i := 0; i &lt; numberOfItems; i++ {
      todoDetail := TodoDetail{
        ID: todo.ID,
        Item: &quot;item &quot;+ strconv.Itoa(i),
      }
      uc.repo.CreateTodoDetail(&amp;todoDetail)
    }
  
    return &quot;success&quot;, nil 
  }
  </code></pre>
  <pre class="lang-golang prettyprint-override"><code>package service
  
  import(
    &quot;testing&quot;
    &quot;github.com/stretchr/testify/assert&quot;
      &quot;github.com/stretchr/testify/mock&quot;
  )
  
  var repository = &amp;TestRepositoryMock{Mock: mock.Mock{}}
  var testService = useCase{repo: repository}
  
  func TestService_SaveTodoSuccess(t *testing.T) {
    todo := Todo{
      Title: &quot;this is title&quot;,
    }
    todoDetail := TodoDetail{
        ID: todo.ID,
        Item: &quot;item 1&quot;,
      }
    repository.Mock.On(&quot;CreateTodo&quot;, &amp;todo).Return(nil)
    repository.Mock.On(&quot;CreateTodoDetail&quot;, &amp;todoDetail).Return(nil).Once()
    result, err := testService.SaveTodo(3)
    assert.Nil(t, err)
      assert.NotNil(t, result)
  }
  </code></pre>
  <p>How to test the multiple call for <code>CreateTodoDetail</code> inside for loop?</p>