简体   繁体   中英

Expectations unmatched : Golang unit test error

I am new to Golang,I am getting below error when I try to run below testcase. Can someone please suggest what am I doing wrong here?

db, mock, _ := sqlmock.New()

    dbConnect, _ := services.DB.Connect(services.Config.Database)
    defer db.Close()

    rows := sqlmock.NewRows([]string{"id", "txid", "nounce", "amount", "confirmations", "fromAddress", "toAddress", "toAccountId", "currencyId",
        "processStateId", "transactionTypeId", "transactionFees", "gasPrice", "gasLimit", "transactionIndex", "logIndex", "gasUsed",
        "status", "blockNumber", "createdAt", "updatedAt"}).
        AddRow("039f79c1-dbb3-4439-b9f7-361c7db6e03f", "0x642ec5ca327d0e64e958b1f9a6ef1feb61f0e541629b3769e854608d00d89744", 0, 0.01, 12, "0x3ee9f8c6a2676f2b0ebc324747253a23b7dbd1b3",
            "0x06fcd7f32e155be895ed9282446864e7397ae40d", "77c93121-5a9a-48fa-8c2f-23ff1debc3df", 2, 3, 2, 0.001, 0.001, 210000, 0, 0, 0, 0, 0, "2021-10-30 07:38:03.299+00", "2021-10-30 07:38:03.299+00").
        AddRow("039f79c1-dbb3-4439-b9f7-361c7db6e03f", "0x642ec5ca327d0e64e958b1f9a6ef1feb61f0e541629b3769e854608d00d89744", 0, 0.01, 12, "0x3ee9f8c6a2676f2b0ebc324747253a23b7dbd1b3",
            "0x06fcd7f32e155be895ed9282446864e7397ae40d", "77c93121-5a9a-48fa-8c2f-23ff1debc3df", 2, 3, 2, 0.001, 0.001, 210000, 0, 0, 0, 0, 0, "2021-10-30 07:38:03.299+00", "2021-10-30 07:38:03.299+00")

    GetTransactionsQuery(dbConnect, "25", "0")
    mock.ExpectQuery("SELECT * FROM BlockchainTransactions limit $1 offset $2").WithArgs("25", "0").WillReturnRows(rows)

    if err := mock.ExpectationsWereMet(); err != nil {
        t.Errorf("there were unfulfilled expectations: %s", err)
    }
--- FAIL: Test_GetTransactionsQuery (7.79s)
    ethtransactions_test.go:51: there were unfulfilled expectations: there is a remaining expectation which was not matched: ExpectedQuery => expecting Query, QueryContext or QueryRow which:
          - matches sql: 'SELECT * FROM BlockchainTransactions limit $1 offset $2'
          - is with arguments:
            0 - 25
            1 - 0
          - should return rows:
            row 0 - [039f79c1-dbb3-4439-b9f7-361c7db6e03f 0x642ec5ca327d0e64e958b1f9a6ef1feb61f0e541629b3769e854608d00d89744 0 0.01 12 0x3ee9f8c6a2676f2b0ebc324747253a23b7dbd1b3 0x06fcd7f32e155be895ed9282446864e7397ae40d 77c93121-5a9a-48fa-8c2f-23ff1debc3df 2 3 2 0.001 0.001 210000 0 0 0 0 0 2021-10-30 07:38:03.299+00 2021-10-30 07:38:03.299+00]
            row 1 - [039f79c1-dbb3-4439-b9f7-361c7db6e03f 0x642ec5ca327d0e64e958b1f9a6ef1feb61f0e541629b3769e854608d00d89744 0 0.01 12 0x3ee9f8c6a2676f2b0ebc324747253a23b7dbd1b3 0x06fcd7f32e155be895ed9282446864e7397ae40d 77c93121-5a9a-48fa-8c2f-23ff1debc3df 2 3 2 0.001 0.001 210000 0 0 0 0 0 2021-10-30 07:38:03.299+00 2021-10-30 07:38:03.299+00]
FAIL

Below is the Function I am trying to mock:


func GetTransactionsQuery(db *sqlx.DB, limit string, offset string) ([]Transactions, error) {

    transactions := []Transactions{}
    Query := `SELECT *
    FROM "BlockchainTransactions"
    LIMIT :limit
    OFFSET :offset`
    params := map[string]interface{}{
        "limit":  limit,
        "offset": offset,
    }

    rows, err := db.NamedQuery(Query, params)

    for rows.Next() {
        transaction := Transactions{}
        rows.StructScan(&transaction)
        transactions = append(transactions, transaction)
    }
    rows.Close()

    if err != nil {
        log.Fatalln(`GetTransactionsQuery: Failed to execute query`, err)
        return transactions, err
    }

    return transactions, nil
}

Edit: I am calling an actual function before I mock data; I am still getting same error.

What @Burak Serdar means is that your expectations must be declared before you actually call your function that triggers mock. It means your test code should be

db, mock, _ := sqlmock.New()

dbConnect, _ := services.DB.Connect(services.Config.Database)
defer db.Close()

rows := sqlmock.NewRows([]string{"id", "txid", "nounce", "amount", "confirmations", "fromAddress", "toAddress", "toAccountId", "currencyId",
    "processStateId", "transactionTypeId", "transactionFees", "gasPrice", "gasLimit", "transactionIndex", "logIndex", "gasUsed",
    "status", "blockNumber", "createdAt", "updatedAt"}).
    AddRow("039f79c1-dbb3-4439-b9f7-361c7db6e03f", "0x642ec5ca327d0e64e958b1f9a6ef1feb61f0e541629b3769e854608d00d89744", 0, 0.01, 12, "0x3ee9f8c6a2676f2b0ebc324747253a23b7dbd1b3",
        "0x06fcd7f32e155be895ed9282446864e7397ae40d", "77c93121-5a9a-48fa-8c2f-23ff1debc3df", 2, 3, 2, 0.001, 0.001, 210000, 0, 0, 0, 0, 0, "2021-10-30 07:38:03.299+00", "2021-10-30 07:38:03.299+00").
    AddRow("039f79c1-dbb3-4439-b9f7-361c7db6e03f", "0x642ec5ca327d0e64e958b1f9a6ef1feb61f0e541629b3769e854608d00d89744", 0, 0.01, 12, "0x3ee9f8c6a2676f2b0ebc324747253a23b7dbd1b3",
        "0x06fcd7f32e155be895ed9282446864e7397ae40d", "77c93121-5a9a-48fa-8c2f-23ff1debc3df", 2, 3, 2, 0.001, 0.001, 210000, 0, 0, 0, 0, 0, "2021-10-30 07:38:03.299+00", "2021-10-30 07:38:03.299+00")

//expect first
mock.ExpectQuery("SELECT * FROM BlockchainTransactions limit $1 offset $2").WithArgs("25", "0").WillReturnRows(rows)
// trigger your code
    GetTransactionsQuery(dbConnect, "25", "0")

if err := mock.ExpectationsWereMet(); err != nil {
    t.Errorf("there were unfulfilled expectations: %s", err)
 }

It's better to go thrugh and understand how to use go-sqlmock .

[Suggestion] Also in your code check your error first

func GetTransactionsQuery(db *sqlx.DB, limit string, offset string) ([]Transactions, error) {

    transactions := []Transactions{}
    Query := `SELECT *
    FROM "BlockchainTransactions"
    LIMIT :limit
    OFFSET :offset`
    params := map[string]interface{}{
        "limit":  limit,
        "offset": offset,
    }

    rows, err := db.NamedQuery(Query, params)
    // check error first
    if err != nil {
        log.Fatalln(`GetTransactionsQuery: Failed to execute query`, err)
        return transactions, err
    }

    defer rows.Close()

    for rows.Next() {
        transaction := Transactions{}
        rows.StructScan(&transaction)
        transactions = append(transactions, transaction)
    }

    return transactions, err
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM