mirror of
https://github.com/tucnak/telebot
synced 2024-11-03 09:40:18 +00:00
tests: implement b.Send() function test
This commit is contained in:
parent
855f6edfce
commit
784432503e
@ -44,7 +44,12 @@ func testRawServer(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func TestRaw(t *testing.T) {
|
||||
_, err := b.Raw("BAD METHOD", nil)
|
||||
b, err := newTestBot()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = b.Raw("BAD METHOD", nil)
|
||||
assert.EqualError(t, err, ErrNotFound.Error())
|
||||
|
||||
_, err = b.Raw("", &testPayload{})
|
||||
|
2
bot.go
2
bot.go
@ -521,7 +521,7 @@ func (b *Bot) Send(to Recipient, what interface{}, options ...interface{}) (*Mes
|
||||
case Sendable:
|
||||
return object.Send(b, to, sendOpts)
|
||||
default:
|
||||
return nil, errors.New("telebot: unsupported sendable")
|
||||
return nil, ErrUnsupportedSendable
|
||||
}
|
||||
}
|
||||
|
||||
|
50
bot_test.go
50
bot_test.go
@ -3,14 +3,24 @@ package telebot
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Cached bot instance to avoid getMe method flooding.
|
||||
var b, _ = newTestBot()
|
||||
const (
|
||||
photoID = "AgACAgIAAxkDAAIBV16Ybpg7l2jPgMUiiLJ3WaQOUqTrAAJorjEbh2TBSPSOinaCHfydQO_pki4AAwEAAwIAA3kAA_NQAAIYBA"
|
||||
)
|
||||
|
||||
var (
|
||||
// required to test send and edit methods
|
||||
chatID, _ = strconv.ParseInt(os.Getenv("CHAT_ID"), 10, 64)
|
||||
|
||||
b, _ = newTestBot() // cached bot instance to avoid getMe method flooding
|
||||
to = &Chat{ID: chatID} // to recipient for send and edit methods
|
||||
)
|
||||
|
||||
func defaultSettings() Settings {
|
||||
return Settings{Token: os.Getenv("TELEBOT_SECRET")}
|
||||
@ -71,8 +81,8 @@ func TestBotStart(t *testing.T) {
|
||||
assert.Panics(t, func() { b.Start() })
|
||||
|
||||
pref := defaultSettings()
|
||||
|
||||
pref.Poller = &LongPoller{}
|
||||
|
||||
b, err := NewBot(pref)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@ -105,7 +115,9 @@ func TestBotStart(t *testing.T) {
|
||||
|
||||
func TestBotIncomingUpdate(t *testing.T) {
|
||||
b, err := newTestBot()
|
||||
assert.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tp := &testPoller{updates: make(chan Update, 1)}
|
||||
b.Poller = tp
|
||||
@ -248,3 +260,33 @@ func TestBotIncomingUpdate(t *testing.T) {
|
||||
time.AfterFunc(100*time.Millisecond, b.Stop)
|
||||
b.Start() // stops after some delay
|
||||
}
|
||||
|
||||
func TestBotSend(t *testing.T) {
|
||||
if chatID == 0 {
|
||||
t.Skip("CHAT_ID is required for Send method test")
|
||||
}
|
||||
|
||||
_, err := b.Send(to, nil)
|
||||
assert.Equal(t, ErrUnsupportedSendable, err)
|
||||
|
||||
_, err = b.Send(nil, t.Name())
|
||||
assert.Error(t, err)
|
||||
|
||||
t.Run("what=string", func(t *testing.T) {
|
||||
msg, err := b.Send(to, t.Name())
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, t.Name(), msg.Text)
|
||||
})
|
||||
|
||||
t.Run("what=Sendable", func(t *testing.T) {
|
||||
photo := &Photo{
|
||||
File: File{FileID: photoID},
|
||||
Caption: t.Name(),
|
||||
}
|
||||
|
||||
msg, err := b.Send(to, photo)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, msg.Photo)
|
||||
assert.Equal(t, photo.Caption, msg.Caption)
|
||||
})
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ type testPoller struct {
|
||||
}
|
||||
|
||||
func (p *testPoller) Poll(b *Bot, updates chan Update, stop chan struct{}) {
|
||||
|
||||
for {
|
||||
select {
|
||||
case upd := <-p.updates:
|
||||
|
@ -28,6 +28,12 @@
|
||||
//
|
||||
package telebot
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
|
||||
var (
|
||||
ErrUnsupportedSendable = errors.New("telebot: unsupported sendable")
|
||||
)
|
||||
|
||||
// These are one of the possible events Handle() can deal with.
|
||||
//
|
||||
// For convenience, all Telebot-provided endpoints start with
|
||||
|
Loading…
Reference in New Issue
Block a user