hedwig-im / hedwig

An Adapter-based Bot Framework for Elixir Applications
MIT License
656 stars 73 forks source link

Document how to create ExUnit tests for a responder #36

Closed docteurklein closed 8 years ago

docteurklein commented 8 years ago

I'm currently having problems to create tests for my user-land responders. I tried to copy Hedwig.Responders.HelpTest:

working responder example:

defmodule Edgar.Responder.ItIsSomething do

  @moduledoc """
  react to it's something
  """

  use Hedwig.Responder

  @usage """
  <text> (it's something) - react to this text with an image
  """
  hear ~r/it's something/i, msg do
    send msg, "http://i3.kym-cdn.com/photos/images/original/000/114/139/tumblr_lgedv2Vtt21qf4x93o1_40020110725-22047-38imqt.jpg"
  end
end

test

defmodule Edgar.Responder.ItIsSomethingTest do
  use Hedwig.RobotCase

  @tag start_robot: true, name: "edgar"
  test "it's something - replies with an image url", %{adapter: adapter, msg: msg} do
    send adapter, {:message, %{msg | text: "it's something"}}
    assert_receive {:message, %{text: text}}
    assert String.equals(text, "http://i3.kym-cdn.com/photos/images/original/000/114/139/tumblr_lgedv2Vtt21qf4x93o1_40020110725-22047-38imqt.jpg")
  end
end

result


  1) test it's something - replies with an image url (Edgar.Responder.ItIsSomethingTest)
     test/edgar/responder/it_is_something.exs:5
     No message matching {:message, %{text: text}} after 100ms.
     The process mailbox is empty.
     stacktrace:
       test/edgar/responder/it_is_something.exs:7

Hope my question is not too dumb :) Cheers o/

docteurklein commented 8 years ago

EDIT: I just noticed you recently (january :)) moved TestRobot and RobotCase in lib, which makes them available when downloaded from hex. It resolves the module loading problem (I updated the issue accordingly), but i still get the same error concerning the empty mailbox.

keathley commented 8 years ago

We sorted this out in Slack but I thought I would add the comments here for historical reasons. You're missing the responders: config in your @tag. It should read something like: @tag start_robot: true, name: "edgar", responders: [{YOUR_MODULE_NAME, []}].

It might be worthwhile to add some documentation about this in the README or in module docs to help others avoid this problem in the future. We also discussed defaulting to the mix config for responders but I'm not sure what the ramifications of that might be. @scrogson would know better then me.