Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with mocking PrepareNamed #921

Open
jayant42 opened this issue Apr 10, 2024 · 0 comments
Open

Issue with mocking PrepareNamed #921

jayant42 opened this issue Apr 10, 2024 · 0 comments

Comments

@jayant42
Copy link

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))

@jayant42 jayant42 changed the title Issue with mocking Issue with mocking PrepareNamed Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant