1
0
mirror of https://github.com/zhashkevych/go-sqlxmock.git synced 2025-06-12 21:47:29 +02:00
This commit is contained in:
gedi
2015-09-10 21:31:35 +03:00
parent febff80c09
commit 081a694b0a
4 changed files with 74 additions and 8 deletions

View File

@ -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 {