Skip to content

Commit fa31f40

Browse files
committed
closes #4
1 parent 2e154ee commit fa31f40

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ import (
153153
// will test that order with a different status, cannot be cancelled
154154
func TestShouldNotCancelOrderWithNonPendingStatus(t *testing.T) {
155155
// open database stub
156-
db, err := sql.Open("mock", "")
156+
db, err := sqlmock.New()
157157
if err != nil {
158158
t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
159159
}
@@ -183,7 +183,7 @@ func TestShouldNotCancelOrderWithNonPendingStatus(t *testing.T) {
183183
// will test order cancellation
184184
func TestShouldRefundUserWhenOrderIsCancelled(t *testing.T) {
185185
// open database stub
186-
db, err := sql.Open("mock", "")
186+
db, err := sqlmock.New()
187187
if err != nil {
188188
t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
189189
}
@@ -221,7 +221,7 @@ func TestShouldRefundUserWhenOrderIsCancelled(t *testing.T) {
221221
// will test order cancellation
222222
func TestShouldRollbackOnError(t *testing.T) {
223223
// open database stub
224-
db, err := sql.Open("mock", "")
224+
db, err := sqlmock.New()
225225
if err != nil {
226226
t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
227227
}
@@ -320,6 +320,10 @@ Visit [godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock)
320320

321321
## Changes
322322

323+
- **2014-04-21** introduce **sqlmock.New()** to open a mock database connection for tests. This method
324+
calls sql.DB.Ping to ensure that connection is open, see [issue](https://github.com/DATA-DOG/go-sqlmock/issues/4).
325+
This way on Close it will surely assert if all expectations are met, even if database was not triggered at all.
326+
The old way is still available, but it is advisable to call db.Ping manually before asserting with db.Close.
323327
- **2014-02-14** RowsFromCSVString is now a part of Rows interface named as FromCSVString.
324328
It has changed to allow more ways to construct rows and to easily extend this API in future.
325329
See [issue 1](https://github.com/DATA-DOG/go-sqlmock/issues/1)

sqlmock.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ func init() {
8585
sql.Register("mock", mock)
8686
}
8787

88+
// New creates sqlmock database connection
89+
// and pings it so that all expectations could be
90+
// asserted on Close.
91+
func New() (db *sql.DB, err error) {
92+
db, err = sql.Open("mock", "")
93+
if err != nil {
94+
return
95+
}
96+
// ensure open connection, otherwise Close does not assert expectations
97+
db.Ping()
98+
return
99+
}
100+
88101
// ExpectBegin expects transaction to be started
89102
func ExpectBegin() Mock {
90103
e := &expectedBegin{}

sqlmock_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ import (
77
"time"
88
)
99

10+
// test the case when db is not triggered and expectations
11+
// are not asserted on close
12+
func TestIssue4(t *testing.T) {
13+
db, err := New()
14+
if err != nil {
15+
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
16+
}
17+
ExpectQuery("some sql query which will not be called").
18+
WillReturnRows(NewRows([]string{"id"}))
19+
20+
err = db.Close()
21+
if err == nil {
22+
t.Errorf("Was expecting an error, since expected query was not matched")
23+
}
24+
}
25+
1026
func TestMockQuery(t *testing.T) {
1127
db, err := sql.Open("mock", "")
1228
if err != nil {

0 commit comments

Comments
 (0)