jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.3k stars 1.09k forks source link

Issue with mocking PrepareNamed #921

Open jayant42 opened 7 months ago

jayant42 commented 7 months ago

How can i mock below.

stmt, err := Service.PrepareNamed(INSERT INTO sample (right_id,created_by) VALUES (:right_id,:created_by) returning id`)

if err != nil {
    return err, 0
}

var id int64
err = stmt.Get(&id, payload)
if err != nil {
    return err, 0
}

type service struct { Connection *sqlx.DB Service }

--Service interface

type Service interface { Query(query string) (sql.Rows, error) PrepareNamed(query string) (sqlx.NamedStmt, error) }

` Error at PrepareNamed as Nil pointer dereference

Test that I have written . Mock.ExpectBegin() requestRow := sqlmock.NewRows([]string{"id"}) requestRow.AddRow(1) Mock.ExpectPrepare("INSERT INTO sample (.+)").ExpectQuery(). WillReturnRows(requestRow) shared_services.Mock.ExpectExec("INSERT INTO sample (.*)").WithArgs( "TEST", "TEST").WillReturnResult(sqlmock.NewResult(100, 1))