tests: add offline mode

pull/279/head
Orivej Desh 4 years ago
parent 04cda01949
commit 95769df144

@ -42,12 +42,16 @@ func NewBot(pref Settings) (*Bot, error) {
client: client, client: client,
} }
user, err := bot.getMe() if pref.offline {
if err != nil { bot.Me = &User{}
return nil, err } else {
user, err := bot.getMe()
if err != nil {
return nil, err
}
bot.Me = user
} }
bot.Me = user
return bot, nil return bot, nil
} }
@ -91,6 +95,9 @@ type Settings struct {
// HTTP Client used to make requests to telegram api // HTTP Client used to make requests to telegram api
Client *http.Client Client *http.Client
// offline allows to create a bot without network for testing purposes.
offline bool
} }
// Update object represents an incoming update. // Update object represents an incoming update.

@ -47,11 +47,7 @@ func TestNewBot(t *testing.T) {
_, err = NewBot(pref) _, err = NewBot(pref)
assert.Error(t, err) assert.Error(t, err)
if token == "" { b, err := NewBot(Settings{offline: true})
t.Skip("TELEBOT_SECRET is required")
}
b, err := newTestBot()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -67,6 +63,7 @@ func TestNewBot(t *testing.T) {
pref.Client = client pref.Client = client
pref.Poller = &LongPoller{Timeout: time.Second} pref.Poller = &LongPoller{Timeout: time.Second}
pref.Updates = 50 pref.Updates = 50
pref.offline = true
b, err = NewBot(pref) b, err = NewBot(pref)
assert.NoError(t, err) assert.NoError(t, err)
@ -135,18 +132,11 @@ func TestBotStart(t *testing.T) {
} }
func TestBotIncomingUpdate(t *testing.T) { func TestBotIncomingUpdate(t *testing.T) {
if token == "" { b, err := NewBot(Settings{Synchronous: true, offline: true})
t.Skip("TELEBOT_SECRET is required")
}
b, err := newTestBot()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
tp := &testPoller{updates: make(chan Update, 1)}
b.Poller = tp
b.Handle("/start", func(m *Message) { b.Handle("/start", func(m *Message) {
assert.Equal(t, "/start", m.Text) assert.Equal(t, "/start", m.Text)
}) })

Loading…
Cancel
Save