November 2018
Intermediate to advanced
346 pages
8h 12m
English
There are three possible errors we need to handle:
So, how do we test for SQL insert failure? With SQLMock it's easy: we make a copy of the previous test and instead of returning sql.Result, we return an error, as shown in the following code:
// configure the mock dbqueryRegex := convertSQLToRegex(sqlInsert)dbMock.ExpectExec(queryRegex).WillReturnError(errors.New("failed to insert"))
We can then change our expectations from a result to an error, as shown in the following code:
require.Error(t, err)assert.Equal(t, defaultPersonID, resultID)assert.NoError(t, dbMock.ExpectationsWereMet())
Moving on to testing ...
Read now
Unlock full access