0

I have the code:

func (db *DB) Confirm(hash string) error {
    var authInfo config.AuthConfirmation
    row := db.QueryRow("SELECT * FROM auth_confirmation WHERE hash = $1", hash)
    err := row.Scan(&authInfo.Email, &authInfo.Hash, &authInfo.Deadline)
    if err != nil {
        return err
    }
// some other logic

I want to test this function using sql-mock

hash := "$2a$04$oA1axCBmazUBEzNl0KjrHuVy.ssgX4oySz/MKZGdoUX5h7vim.TfG"
row := mock.NewRows([]string{"email", "hash", "deadline"}).
        AddRow("example@ex.com", hash, time.Now().Add(time.Hour))

mock.ExpectQuery("SELECT * FROM auth_confirmation").
        WithArgs(hash).WillReturnRows(row)

But Scan returns error:

Query: could not match actual sql: "SELECT * FROM auth_confirmation WHERE hash = $1" with expected regexp "SELECT * FROM auth_confirmation"

What mistakes I did?

SerGO
  • 21
  • 5
  • Since it claims to be a regex, you probably need to escape your `*`: `mock.ExpectQuery("SELECT \\* FROM auth_confirmation").` Otherwise ` *` is interpreted as "zero or more spaces". – Jonathan Hall Oct 22 '20 at 09:50
  • 1
    Have you already see https://stackoverflow.com/questions/59652031/sqlmock-is-not-matching-query-but-query-is-identical-and-log-output-shows-the-s – Matteo Oct 22 '20 at 09:51

0 Answers0