alice v0.4.3 Alice.HandlerCase

Helpers for writing tests of Alice Handlers.

When used it accepts the following options:

  • :handlers - The handler (or List of handlers) that you want to test. Defaults to [] (thereby giving you no handlers to test)

useing this handler automatically brings in ExUnit.Case as well.

Examples

defmodule Alice.Handlers.ExampleHandlerTest do
  use Alice.HandlerCase, handlers: Alice.Handlers.ExampleHandler

  test "it replies" do
    send_message("hello")
    assert first_reply() == "world"
  end
end

Link to this section Summary

Functions

Retrieves a List of all the replies that Alice has sent out since the test began.

Generates a fake connection for testing purposes.

Generates a fake connection for testing purposes.

Generates a fake connection for testing purposes.

Retrieves the first reply that Alice sent out since the test began.

Sends a message through Alice that can be captured by the handlers.

Verifies that typing was indicated during the test.

Link to this section Types

Link to this type

conn()

conn() :: %Alice.Conn{message: term(), slack: term(), state: term()}

Link to this section Functions

Link to this function

all_replies()

all_replies() :: [String.t()]

Retrieves a List of all the replies that Alice has sent out since the test began.

Examples

test "you can send multiple messages" do
  send_message("first")
  send_message("second")
  assert all_replies() == ["first", "second"]
end
Link to this function

fake_conn()

fake_conn() :: conn()

Generates a fake connection for testing purposes.

Can be called as fake_conn/0 to generate a quick connection.

Examples

test "you can directly use the reply function" do
  conn = fake_conn()
  reply(conn, "hello world")
  assert first_reply() == "hello world"
end
Link to this function

fake_conn(text)

fake_conn(String.t()) :: conn()

Generates a fake connection for testing purposes.

Can be called as fake_conn/1 to pass a message.

Examples

test "you can set the message in the conn" do
  conn = fake_conn("message")
  send_message(conn)
  assert first_reply() == "hello world"
end
Link to this function

fake_conn(text, list)

fake_conn(String.t(), opts :: [{:state, map()}]) :: conn()
fake_conn(String.t(), opts :: [{:capture, Regex.t()}]) :: conn()

Generates a fake connection for testing purposes.

Can be called as fake_conn/2 to set options. Options can either be :state or :capture, but not both. Using :capture is helpful if you want to unit test your handler functions.

Examples

test "you can set state" do
  conn = fake_conn("message", state: %{some: "state"})
  conn = send_message(conn)
  assert first_reply() == "hello world"
  assert conn.state.some == "state"
end

test "you can set the regix and call the handler directly" do
  conn = fake_conn("message", ~r"^(.+)")
  MyHandler.do_something(conn)
  assert first_reply() == "hello world, you said 'message'"
end
Link to this function

first_reply()

first_reply() :: String.t()

Retrieves the first reply that Alice sent out since the test began.

Examples

test "it only brings back the first message" do
  send_message("first")
  send_message("second")
  assert first_reply() == "first"
end
Link to this function

send_message(conn)

send_message(String.t() | conn()) :: conn()

Sends a message through Alice that can be captured by the handlers.

Can either be called with a message String or Alice.Conn

## Examples

  test "it sends a message" do
    send_message("test message")
    assert first_reply() == "reply from handler"
  end

  test "it sends a message with a conn" do
    conn = fake_conn("test message")
    send_message(conn)
    assert first_reply() == "reply from handler"
  end
Link to this function

typing?()

typing?() :: boolean()

Verifies that typing was indicated during the test.

Examples

test "the handler indicated typing" do
  send_message("message that causes the handler to indicate typing")
  assert typing?
end