diff --git a/README.md b/README.md index be88def..502e300 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ import ( // will test that order with a different status, cannot be cancelled func TestShouldNotCancelOrderWithNonPendingStatus(t *testing.T) { // open database stub - db, err := sql.Open("mock", "") + db, err := sqlmock.New() if err != nil { t.Errorf("An error '%s' was not expected when opening a stub database connection", err) } @@ -183,7 +183,7 @@ func TestShouldNotCancelOrderWithNonPendingStatus(t *testing.T) { // will test order cancellation func TestShouldRefundUserWhenOrderIsCancelled(t *testing.T) { // open database stub - db, err := sql.Open("mock", "") + db, err := sqlmock.New() if err != nil { t.Errorf("An error '%s' was not expected when opening a stub database connection", err) } @@ -221,7 +221,7 @@ func TestShouldRefundUserWhenOrderIsCancelled(t *testing.T) { // will test order cancellation func TestShouldRollbackOnError(t *testing.T) { // open database stub - db, err := sql.Open("mock", "") + db, err := sqlmock.New() if err != nil { t.Errorf("An error '%s' was not expected when opening a stub database connection", err) } @@ -320,6 +320,10 @@ Visit [godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock) ## Changes +- **2014-04-21** introduce **sqlmock.New()** to open a mock database connection for tests. This method +calls sql.DB.Ping to ensure that connection is open, see [issue](https://github.com/DATA-DOG/go-sqlmock/issues/4). +This way on Close it will surely assert if all expectations are met, even if database was not triggered at all. +The old way is still available, but it is advisable to call db.Ping manually before asserting with db.Close. - **2014-02-14** RowsFromCSVString is now a part of Rows interface named as FromCSVString. It has changed to allow more ways to construct rows and to easily extend this API in future. See [issue 1](https://github.com/DATA-DOG/go-sqlmock/issues/1) diff --git a/sqlmock.go b/sqlmock.go index 15660a0..2386363 100644 --- a/sqlmock.go +++ b/sqlmock.go @@ -85,6 +85,19 @@ func init() { sql.Register("mock", mock) } +// New creates sqlmock database connection +// and pings it so that all expectations could be +// asserted on Close. +func New() (db *sql.DB, err error) { + db, err = sql.Open("mock", "") + if err != nil { + return + } + // ensure open connection, otherwise Close does not assert expectations + db.Ping() + return +} + // ExpectBegin expects transaction to be started func ExpectBegin() Mock { e := &expectedBegin{} diff --git a/sqlmock_test.go b/sqlmock_test.go index 59deb74..a7d2d61 100644 --- a/sqlmock_test.go +++ b/sqlmock_test.go @@ -7,6 +7,22 @@ import ( "time" ) +// test the case when db is not triggered and expectations +// are not asserted on close +func TestIssue4(t *testing.T) { + db, err := New() + if err != nil { + t.Errorf("an error '%s' was not expected when opening a stub database connection", err) + } + ExpectQuery("some sql query which will not be called"). + WillReturnRows(NewRows([]string{"id"})) + + err = db.Close() + if err == nil { + t.Errorf("Was expecting an error, since expected query was not matched") + } +} + func TestMockQuery(t *testing.T) { db, err := sql.Open("mock", "") if err != nil {