You've already forked go-sqlxmock
mirror of
https://github.com/zhashkevych/go-sqlxmock.git
synced 2025-06-12 21:47:29 +02:00
closes #24
This commit is contained in:
@ -3,8 +3,14 @@ package sqlmock
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type void struct{}
|
||||
|
||||
func (void) Print(...interface{}) {}
|
||||
|
||||
func ExampleNew() {
|
||||
db, mock, err := New()
|
||||
if err != nil {
|
||||
@ -16,6 +22,33 @@ func ExampleNew() {
|
||||
mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call"))
|
||||
}
|
||||
|
||||
func ExampleNewWithDSN() {
|
||||
_, mock, err := NewWithDSN("mydsn for gorm")
|
||||
if err != nil {
|
||||
fmt.Println("expected no error, but got:", err)
|
||||
return
|
||||
}
|
||||
// use the same dsn to initialize a connection
|
||||
db, err := gorm.Open("sqlmock", "mydsn for gorm")
|
||||
if err != nil {
|
||||
fmt.Println("expected no error from gorm, but got:", err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetLogger(void{}) // sad - nil panics
|
||||
|
||||
// now we can expect operations performed on db
|
||||
mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call"))
|
||||
|
||||
txconn := db.Begin()
|
||||
for _, err := range txconn.GetErrors() {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
// Output: `sqlmock` is not officially supported, running under compatibility mode.
|
||||
// an error will occur on db.Begin() call
|
||||
}
|
||||
|
||||
func TestShouldOpenConnectionIssue15(t *testing.T) {
|
||||
db, mock, err := New()
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user