mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-03-19 20:57:50 +02:00
Merge pull request #326 from co60ca/master
Modify: existing panic in AddRow to give a hint to the issue
This commit is contained in:
commit
b2d135c5e4
2
rows.go
2
rows.go
@ -166,7 +166,7 @@ func (r *Rows) RowError(row int, err error) *Rows {
|
|||||||
// of columns
|
// of columns
|
||||||
func (r *Rows) AddRow(values ...driver.Value) *Rows {
|
func (r *Rows) AddRow(values ...driver.Value) *Rows {
|
||||||
if len(values) != len(r.cols) {
|
if len(values) != len(r.cols) {
|
||||||
panic("Expected number of values to match number of columns")
|
panic(fmt.Sprintf("Expected number of values to match number of columns: expected %d, actual %d", len(values), len(r.cols)))
|
||||||
}
|
}
|
||||||
|
|
||||||
row := make([]driver.Value, len(r.cols))
|
row := make([]driver.Value, len(r.cols))
|
||||||
|
25
rows_test.go
25
rows_test.go
@ -730,6 +730,31 @@ func TestAddRows(t *testing.T) {
|
|||||||
// scanned id: 4 and title: Emily
|
// scanned id: 4 and title: Emily
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddRowExpectPanic(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
const expectedPanic = "Expected number of values to match number of columns: expected 1, actual 2"
|
||||||
|
values := []driver.Value{
|
||||||
|
"John",
|
||||||
|
"Jane",
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
if r != expectedPanic {
|
||||||
|
t.Fatalf("panic message did not match expected: expected '%s', actual '%s'", r, expectedPanic)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Fatalf("expected panic: %s", expectedPanic)
|
||||||
|
}()
|
||||||
|
|
||||||
|
rows := NewRows([]string{"id", "name"})
|
||||||
|
// Note missing spread "..."
|
||||||
|
rows.AddRow(values)
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleRows_AddRows() {
|
func ExampleRows_AddRows() {
|
||||||
db, mock, err := New()
|
db, mock, err := New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user