vendredi 14 juin 2019

DB Test failing: Regexp doesn't match

I have following code which is inserting into a table

func (s *MyRepo) InsertValue(ctx context.Context, orderID string) error {
    _, err := s.db.DatabaseBuilder().
        WithContext(ctx).
        DeleteFrom("orders").
        Where("orderID=?", orderID).
        Exec()
    return err
}

To test this code, I wrote following test

func TestMyRepo_InsertValue_Error(t *testing.T) {
    orderID := "ORDERID"
    dbError := "DB_ERROR"
    mockDB, repo := getDBStore()

    mockDB.ExpectExec("INSERT INTO `orders` (`orderID`) VALUES (?)").
        WithArgs(orderID).
        WillReturnError(errors.New(dbError))

    err := repo.InsertValue(context.Background(), orderID)
    assert.Equal(t, dbError, err.Error())
}

For this, I am getting error that says

+ExecQuery 'INSERT INTO `orders` (`orderID`) VALUES (?)', does not match regex 'INSERT INTO `orders` (`orderID`) VALUES (?)'

The two SQL statements in the error are same (at least thats what appears to me). Am i missing something? How would i test my insert function here

Aucun commentaire:

Enregistrer un commentaire