title: How to mock gorm insert with go-sql (postgres)
body: <p>I'm using <a href="https://gorm.io/" rel="noreferrer">Gorm</a> with the postgresql driver. I try to mock a database insert with <a href="https://github.com/DATA-DOG/go-sqlmock" rel="noreferrer">go-sqlmock</a>:</p>
  
  <pre><code>type Test struct {
      FirstName string `json:"first_name"`
  }
  
  func (db *DB) CreateTest() (*Test, error) {
      test := Test{"c"}
      if result := db.Create(&amp;test); result.Error != nil {
          return nil, result.Error
      }
  
      return &amp;test, nil
  }
  
  
  func TestCreateUser(t *testing.T) {
      _, mock, _ := sqlmock.NewWithDSN("sqlmock_db_0")
  
      mockedGorm, _ := gorm.Open("sqlmock", "sqlmock_db_0")
      defer mockedGorm.Close()
      myDB := &amp;DB{mockedGorm}
  
      mock.ExpectExec("INSERT INTO test").WithArgs("c").WillReturnResult(sqlmock.NewResult(1, 1))
      myDB.Exec("INSERT INTO test(first_name) VALUES (?)", "c")
  
  
      if _, err := myDB.CreateTest(); err != nil {
          t.Errorf("error was not expected: %s", err)
      }
  
      if err := mock.ExpectationsWereMet(); err != nil {
          t.Errorf("there were unfulfilled expectations: %s", err)
      }
  }
  </code></pre>
  
  <p>Unfortunately, this gives me an error:</p>
  
  <pre><code>error was not expected: all expectations were already fulfilled, call to database transaction Begin was not expected
  </code></pre>
  
  <p>How do I test an insert with gorm, postgresql and sql-mock properly?</p>