joe: github.com/go-joe/joe/joetest Index | Files

package joetest

import "github.com/go-joe/joe/joetest"

Package joetest implements helpers to implement unit tests for bots.

Index

Package Files

bot.go brain.go storage.go testing.go

type Bot Uses

type Bot struct {
    *joe.Bot
    T       TestingT
    Input   io.Writer
    Output  io.Reader
    Timeout time.Duration // defaults to 1s
    // contains filtered or unexported fields
}

Bot wraps a *joe.Bot for unit testing.

func NewBot Uses

func NewBot(t TestingT, modules ...joe.Module) *Bot

NewBot creates a new *Bot instance that can be used in unit tests. The Bots will use a CLIAdapter which accepts messages from the Bot.Input and write all output to Bot.Output. The logger is a zaptest.Logger which sends all logs through the passed TestingT (usually a *testing.T instance).

For ease of testing a Bot can be started and stopped without a cancel via Bot.Start() and Bot.Stop().

func (*Bot) EmitSync Uses

func (b *Bot) EmitSync(event interface{})

EmitSync emits the given event on the Brain and blocks until all registered handlers have completely processed it.

func (*Bot) ReadOutput Uses

func (b *Bot) ReadOutput() string

ReadOutput consumes all data from b.Output and returns it as a string so you can easily make assertions on it.

func (*Bot) Run Uses

func (b *Bot) Run() error

Run wraps Bot.Run() in order to allow stopping a Bot without having to inject another context.

func (*Bot) Start Uses

func (b *Bot) Start()

Start executes the Bot.Run() function and stores its error result in a channel so the caller can eventually execute Bot.Stop() and receive the result. This function blocks until the event handler is actually running and emits the InitEvent.

func (*Bot) Stop Uses

func (b *Bot) Stop()

Stop stops a running Bot and blocks until it has completed. If Bot.Run() returned an error it is passed to the Errorf function of the TestingT that was used to create the Bot.

type Brain Uses

type Brain struct {
    *joe.Brain
    // contains filtered or unexported fields
}

Brain wraps the joe.Brain for unit testing.

func NewBrain Uses

func NewBrain(t TestingT) *Brain

NewBrain creates a new Brain that can be used for unit testing. The Brain registers to all events except the (init and shutdown event) and records them for later access. The event handling loop of the Brain (i.e. Brain.HandleEvents()) is automatically started by this function in a new goroutine and the caller must call Brain.Finish() at the end of their tests.

func (*Brain) Events Uses

func (b *Brain) Events() <-chan joe.Event

Events returns a channel that receives all emitted events.

func (*Brain) Finish Uses

func (b *Brain) Finish()

Finish stops the event handler loop of the Brain and waits until all pending events have been processed.

func (*Brain) RecordedEvents Uses

func (b *Brain) RecordedEvents() []interface{}

RecordedEvents returns all events the Brain has processed except the joe.InitEvent and joe.ShutdownEvent.

type Storage Uses

type Storage struct {
    *joe.Storage
    T   TestingT
}

Storage wraps a joe.Storage for unit testing purposes.

func NewStorage Uses

func NewStorage(t TestingT) *Storage

NewStorage creates a new Storage.

func (*Storage) AssertEquals Uses

func (s *Storage) AssertEquals(key string, expectedVal interface{})

AssertEquals checks that the actual value under the given key equals an expected value.

func (*Storage) MustSet Uses

func (s *Storage) MustSet(key string, value interface{})

MustSet assigns the value to the given key and fails the test immediately if there was an error.

type TestingT Uses

type TestingT interface {
    Logf(string, ...interface{})
    Errorf(string, ...interface{})
    Fail()
    Failed() bool
    Fatal(args ...interface{})
    Name() string
    FailNow()
    Helper()
}

TestingT is the minimum required subset of the testing API used in the joetest package. TestingT is implemented both by *testing.T and *testing.B.

Package joetest imports 9 packages (graph). Updated 2019-11-15. Refresh now. Tools for package owners.