0

我想做一个 100% 覆盖率的单元测试,但是在单元测试中遗漏了许多命令或检查,我该如何修复它才能达到 100% 覆盖率

func (db *Database) SaveBook(id string, url string, username string, email string, password string, data string) error {
    sqlStatement := "INSERT INTO book (id, cerator,name_book) VALUES (?, ?, ?)"
    stmt, err := db.DB.Prepare(sqlStatement)
    if err != nil {
        return err
    }

    result, errExec := stmt.Exec(id, url, username, email, password, data)
    if errExec != nil {
        return errExec
    }
    rows, err := result.RowsAffected()
    if err != nil {
        return err
    }
    if rows != 1 {
        return err
    }

    return nil
}

对于单元测试示例,我使用它。这有什么问题

func TestSavetWsSuccesss(t *testing.T) {
    db, mock := NewMock()
    repo := data.NewDatabase(db)
    query := "INSERT INTO book (id, cerator,name_book) VALUES (?, ?, ?)"
    prep := mock.ExpectPrepare(regexp.QuoteMeta(query))
    prep.ExpectExec().WithArgs(id,creator,name).WillReturnError(errors.New("sql error"))
}
4

0 回答 0