Skip to content

Commit

Permalink
Test Segment tracking after user creation
Browse files Browse the repository at this point in the history
  • Loading branch information
vidmantas committed Oct 16, 2016
1 parent 88839d9 commit 9c821c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ config :code_corps, allowed_origins: ["http://localhost:4200"]
config :guardian, Guardian,
secret_key: "e62fb6e2746f6b1bf8b5b735ba816c2eae1d5d76e64f18f3fc647e308b0c159e"

config :code_corps, :analytics, CodeCorps.Analytics.InMemoryAPI
config :code_corps, :analytics, CodeCorps.Analytics.TestAPI

config :code_corps, :icon_color_generator, CodeCorps.RandomIconColor.TestGenerator

Expand Down
18 changes: 18 additions & 0 deletions lib/code_corps/analytics/test_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule CodeCorps.Analytics.TestAPI do
@moduledoc """
In-memory interface to simulate calling out to the Segment API,
sending back itself a message with passed parameters - they're used for assertions.
Each function should have the same signature as `CodeCorps.Analytics.SegmentAPI` and simply return `nil`.
"""

def identify(user_id, traits) do
send self(), {:identify, user_id, traits}
nil
end

def track(user_id, event_name, properties) do
send self(), {:track, user_id, event_name, properties}
nil
end
end
14 changes: 14 additions & 0 deletions test/controllers/user_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ defmodule CodeCorps.UserControllerTest do
assert user.id == slugged_route.user_id
end

test "calls segment tracking after user is created", %{conn: conn} do
conn = post conn, user_path(conn, :create), %{
"meta" => %{},
"data" => %{
"type" => "user",
"attributes" => Map.put(@valid_attrs, :password, "password"),
"relationships" => relationships
}
}
id = json_response(conn, 201)["data"]["id"]

assert_received {:track, id, "Signed Up", %{}}
end

test "does not create resource and renders errors when data is invalid", %{conn: conn} do
conn = post conn, user_path(conn, :create), %{
"meta" => %{},
Expand Down

0 comments on commit 9c821c1

Please sign in to comment.