Package joetest implements helpers to implement unit tests for bots.



    This section is empty.


    This section is empty.


    This section is empty.


    type Bot

    type Bot struct {
    	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

      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

        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

          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

            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

              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

                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

                  type Brain struct {
                  	// contains filtered or unexported fields

                    Brain wraps the joe.Brain for unit testing.

                    func NewBrain

                    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

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

                        Events returns a channel that receives all emitted events.

                        func (*Brain) Finish

                        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

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

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

                            type Storage

                            type Storage struct {
                            	T TestingT

                              Storage wraps a joe.Storage for unit testing purposes.

                              func NewStorage

                              func NewStorage(t TestingT) *Storage

                                NewStorage creates a new Storage.

                                func (*Storage) AssertEquals

                                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

                                  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

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

                                      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.