JosiahWitt / ensure

A balanced test framework for Go
MIT License
8 stars 0 forks source link

Add support for mocks in tables #6

Closed JosiahWitt closed 3 years ago

JosiahWitt commented 3 years ago

Integrates with gomock.

For example:

type Mocks struct {
  DB *mock_db.MockDB // Generated by gomock
}

table := []struct {
  Name    string
  Input   string
  IsEmpty bool

  Mocks      *Mocks
  SetupMocks func(*Mocks)
}{
  {
    Name:    "with non empty input",
    Input:   "my string",
    IsEmpty: false,
    SetupMocks: func(mocks *Mocks) {
      mocks.DB.EXPECT().Set("my-key", "my value")
    },
  },
  {
    Name:    "with empty input",
    Input:   "",
    IsEmpty: true,
  },
}

ensure.RunTableByIndex(table, func(ensure Ensure, i int) {
  entry := table[i]

  // entry.Mocks.DB is ready for use here

  isEmpty := strs.IsEmpty(entry.Input)
  ensure(isEmpty).Equals(entry.IsEmpty)
})