DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.05k stars 406 forks source link

Problem while INSERTING #224

Open RajivBobble opened 4 years ago

RajivBobble commented 4 years ago

This is the Error I'm getting all expectations were already fulfilled, call to database transaction Begin was not expected

I have a code in my go lang

var userRoles models.UserRole
userRoles.UserID = userID
userRoles.RoleID = id
db.Create(&userRoles)

And This Query is generated INSERT INTO user_roles (user_id,role_id,created_at,updated_at) VALUES (1,1,2020-05-08 13:35:59,2020-05-08 13:35:59) While mocking with this code with WithArgs() and Without WithArgs()

mock.ExpectBegin()
mock.ExpectExec("INSERT INTO \"u").WithArgs(1, 1, sqlmock.AnyArg(), sqlmock.AnyArg()).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

I have also tried this mock.ExpectQuery("INSERT INTO user.*").WithArgs(1,1,sqlmock.AnyArg()sqlmock.AnyArg()).WillReturnRows(row) Getting same error

rmulley commented 3 years ago

@RajivBobble I'm happy to help, but I'll need a more complete example. Could you provide more of the relevant code?