Created
February 26, 2016 11:14
-
-
Save jamiecuthill/2497abae37586a9d4fff to your computer and use it in GitHub Desktop.
sqlmock false positive on Exec with ExpectationsWereMet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import "database/sql" | |
func RunExecs(db *sql.DB) { | |
_, _ = db.Exec("THE FIRST EXEC") | |
_, _ = db.Exec("THE WRONG EXEC") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// All these tests should fail | |
package example_test | |
import ( | |
"testing" | |
"github.com/DATA-DOG/go-sqlmock" | |
"github.com/SainsburysNDA/example" | |
) | |
// False Positive - passes despite mismatched Exec | |
func TestRunExecsWithOrdered(t *testing.T) { | |
db, dbmock, _ := sqlmock.New() | |
dbmock.MatchExpectationsInOrder(true) // This is the default | |
dbmock.ExpectExec("THE FIRST EXEC") | |
dbmock.ExpectExec("THE SECOND EXEC") | |
example.RunExecs(db) | |
err := dbmock.ExpectationsWereMet() | |
if err != nil { | |
t.Fatal(err) | |
} | |
} | |
// Fails as expected | |
func TestRunExecsWithouOrdered(t *testing.T) { | |
db, dbmock, _ := sqlmock.New() | |
dbmock.MatchExpectationsInOrder(false) | |
dbmock.ExpectExec("THE FIRST EXEC") | |
dbmock.ExpectExec("THE SECOND EXEC") | |
example.RunExecs(db) | |
err := dbmock.ExpectationsWereMet() | |
if err != nil { | |
t.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment