mercredi 30 novembre 2016

How to send data into a channel for testing purposes in Golang?

I'm trying to write unit tests for functions for a project in Go and I'm coming up against a problem I've never encountered before. The function is used in a loop that monitors Slack (the messaging platform) live for certain event structures (defined by a library I'm using) and responds according depending on the event returned (using a switch). Here's (most of) the code:

func botLoop(s *SlackBot) {
    select {
    case rtmEvent := <-s.Rtm.IncomingEvents:
        switch ev := rtmEvent.Data.(type) {
        case *slack.MessageEvent:

            o, err := s.HandleCommand(ev)
            if err != nil {
                fmt.Printf("%s\n", err)
                s.Say(ev.Channel, "%s\n", err)
                break
            }
            s.Say(ev.Channel, o)

         case *slack.LatencyReport:
             fmt.Printf("Current latency: %v\n", ev.Value)

         default:
             // fmt.Printf("Unexpected: %v\n", msg.Data)
    }
}

How can I pass "rtmEvents" into the s.Rtm.IncomingEvent channel to trigger my code for testing purposes? Is there any way to reliably do this?

Here's the documentation for the API library I'm using, if that makes things any easier.

Aucun commentaire:

Enregistrer un commentaire