0

到这里。试图弄清楚如何使用SQL mock v2

这是我的界面:

type OrderPersister interface {
    FetchOrderById(string) (*Order, error)
}

我对该接口的实现:

type DbPersister struct {
    Config config.DbConfig
    GormDB *gorm.DB
}

func (op DbPersister) FetchOrderById(orderId string) (*Order, error) {
    Order := &Order{}

    orderUuid, err := uuid.Parse(orderId)
    if err != nil {
        return nil, err
    }

    if err := op.GormDB.Table("orders").
        Select(`orders.order_id,
            orders.user_id,
            orders.quantity,
            orders.status
            addresses.line_1,
            users.email`).
        Joins("join addresses on addresses.address_id = orders.address_id").
        Joins("join users on users.user_id = orders.user_id").
        Where("orders.order_id = ?", orderUuid).
        First(Order).Error; err != nil {
        return nil, err
    }

    return Order, nil
}

我的单元测试(包括设置/初始化):

import (
    "database/sql"
    "testing"

    "github.com/google/uuid"
    "github.com/jinzhu/gorm"
    "github.com/stretchr/testify/require"
    "github.com/stretchr/testify/suite"
    "gopkg.in/DATA-DOG/go-sqlmock.v2"
)

type Suite struct {
    suite.Suite
    DB   *gorm.DB
    mock sqlmock.Sqlmock

    dbPersister OrderPersister
}

func (s *Suite) SetupSuite() {
    var (
        db  *sql.DB
        err error
    )

    db, s.mock, err = sqlmock.New()
    require.NoError(s.T(), err)

    s.DB, err = gorm.Open("postgres", db)
    require.NoError(
        s.T(), err)

    s.DB.LogMode(true)

    s.dbPersister = DbPersister{
        Config: config.DbConfig{
            DbHost:     "",
            DbPort:     "",
            DbName:     "",
            DbUsername: "",
            DbPassword: "",
        },
        GormDB: s.DB,
    }
}

func (s *Suite) BeforeTest(_, _ string) {
    var (
        db  *sql.DB
        err error
    )

    db, s.mock, err = sqlmock.New()
    require.NoError(s.T(), err)

    s.DB, err = gorm.Open("postgres", db)
    require.NoError(s.T(), err)

    s.DB.LogMode(true)
}

func (s *Suite) AfterTest(_, _ string) {
    require.NoError(s.T(), s.mock.ExpectationsWereMet())
}

func TestInit(t *testing.T) {
    suite.Run(t, new(Suite))
}

func (s *Suite) TestFetchOrderById() {

  // given
    orderId := uuid.New()
  quantity := 1
  status := "ready"
    line1 := "201"
  email := "jsmith@example.com"

  // s.mock.ExpectBegin()
    s.mock.ExpectQuery(`SELECT`).
        WillReturnRows(sqlmock.NewRows([]string{"orders.order_id","orders.user_id","orders.quantity","orders.status",
                "addresses.line_1","user_logins.email"}).
            AddRow(sqlmock.AnyArg(), sqlmock.AnyArg(), quantity, status, totalExclTax, shippingExclTax,
                totalTaxAmt, line1, state, zip, locality, upc, email, firstName, lastName))
    _, err := s.dbPersister.FetchOrderById(orderId.String())
  s.mock.ExpectCommit()
    require.NoError(s.T(), err)
}

当它运行时,测试失败,原因如下:

--- FAIL: TestInit (0.00s)
  --- FAIL: TestInit/TestFetchOrderById (0.00s)
    db_test.go:67: 
        Error Trace:    db_test.go:67
                                suite.go:137
                                panic.go:969
                                rows.go:134
                                db_test.go:99
        Error:          Received unexpected error:
                        there is a remaining expectation which was not matched: ExpectedQuery => expecting Query, QueryContext or QueryRow which:
                          - matches sql: 'SELECT'
                          - is without arguments
        Test:           TestInit/TestFetchOrderById

我要做的就是确认使用函数GormDB中指定的 SELECT 语句查询了该实例FetchOrderById

有人知道我需要做什么才能实现这一目标并通过测试吗?

4

1 回答 1

-1

我决定改用 Java(没有双关语)。

于 2020-09-25T14:22:07.747 回答