Skip to content

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

Closed
@mathiasringhof

Description

@mathiasringhof

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions