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

Expectations are not checked if database is not used at all #4

Closed
mathiasringhof opened this issue Apr 17, 2014 · 3 comments
Closed

Comments

@mathiasringhof
Copy link

The following test will pass even if it shouldn't:

func TestSomething(t *testing.T) {
    db, err := sql.Open("mock", "")
    if err != nil {
        t.Fatalf("Could not open mock database: %v", err)
    }
    sqlmock.ExpectExec("DELETE foo").WillReturnResult(sqlmock.NewResult(1, 1))
    if err = db.Close(); err != nil {
        t.Fatalf("Could not close database: %v", err)
    }
}

If I do something with the database, this will fail as expected:

func TestSomething(t *testing.T) {
    db, err := sql.Open("mock", "")
    if err != nil {
        t.Fatalf("Could not open mock database: %v", err)
    }
    sqlmock.ExpectExec("DELETE foo").WillReturnResult(sqlmock.NewResult(1, 1))
    db.Begin() //db.Exec works as well
    if err = db.Close(); err != nil {
        t.Fatalf("Could not close database: %v", err)
    }
}
@l3pp4rd
Copy link
Member

l3pp4rd commented Apr 17, 2014

Hi, looks like the driver.Close is not triggered. will add this test into suite and check what actions could be applied. thanks for the details

@mathiasringhof
Copy link
Author

Thanks! Just discovered that a simple db.Ping() works and is a nice workaround.

@l3pp4rd
Copy link
Member

l3pp4rd commented Apr 21, 2014

Have added a method sqlmock.New() which opens a mock connections and pings it. Using it as a shortcut, will save you from this trouble. The issue was that sql driver Close was not triggered, looks like connection is not being opened unless a database is used. There is no direct hook to ping the database otherwise from the driver side. While keeping all backwards compatible, this should handle the case, could not think of something more convenient, if you have any ideas, you are welcome.

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

2 participants