Skip to content

Commit 4ab7090

Browse files
authored
Merge pull request #115 from eklmv/114-mandatory-withargs
Make arguments matching using WithArgs mandatory, fixes #114
2 parents a544414 + b32be77 commit 4ab7090

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

expectations.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,6 @@ func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery {
376376
}
377377

378378
func (e *queryBasedExpectation) argsMatches(args []interface{}) error {
379-
if nil == e.args {
380-
return nil
381-
}
382379
if len(args) != len(e.args) {
383380
return fmt.Errorf("expected %d, but got %d arguments", len(e.args), len(args))
384381
}

expectations_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,17 @@ func TestQueryRowScan(t *testing.T) {
9595
t.Error(err)
9696
}
9797
}
98+
99+
func TestMissingWithArgs(t *testing.T) {
100+
mock, _ := NewConn()
101+
// No arguments expected
102+
mock.ExpectExec("INSERT something")
103+
// Receiving argument
104+
_, err := mock.Exec(context.Background(), "INSERT something", "something")
105+
if err == nil {
106+
t.Error("arguments do not match error was expected")
107+
}
108+
if err := mock.ExpectationsWereMet(); err == nil {
109+
t.Error("expectation was not matched error was expected")
110+
}
111+
}

pgxmock_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ func TestPrepareExec(t *testing.T) {
959959
mock.ExpectBegin()
960960
ep := mock.ExpectPrepare("foo", "INSERT INTO ORDERS\\(ID, STATUS\\) VALUES \\(\\?, \\?\\)")
961961
for i := 0; i < 3; i++ {
962-
ep.ExpectExec().WillReturnResult(NewResult("UPDATE", 1))
962+
ep.ExpectExec().WithArgs(AnyArg(), AnyArg()).WillReturnResult(NewResult("UPDATE", 1))
963963
}
964964
mock.ExpectCommit()
965965
tx, _ := mock.Begin(context.Background())
@@ -1071,7 +1071,7 @@ func TestPreparedStatementCloseExpectation(t *testing.T) {
10711071
defer mock.Close(context.Background())
10721072

10731073
ep := mock.ExpectPrepare("foo", "INSERT INTO ORDERS").WillBeClosed()
1074-
ep.ExpectExec().WillReturnResult(NewResult("UPDATE", 1))
1074+
ep.ExpectExec().WithArgs(AnyArg(), AnyArg()).WillReturnResult(NewResult("UPDATE", 1))
10751075

10761076
_, err = mock.Prepare(context.Background(), "foo", "INSERT INTO ORDERS(ID, STATUS) VALUES (?, ?)")
10771077
if err != nil {
@@ -1101,7 +1101,7 @@ func TestExecExpectationErrorDelay(t *testing.T) {
11011101

11021102
// test that return of error is delayed
11031103
delay := time.Millisecond * 100
1104-
mock.ExpectExec("^INSERT INTO articles").
1104+
mock.ExpectExec("^INSERT INTO articles").WithArgs(AnyArg()).
11051105
WillReturnError(errors.New("slow fail")).
11061106
WillDelayFor(delay)
11071107

0 commit comments

Comments
 (0)