Skip to content

Commit

Permalink
Merge pull request #11 from wise-home/add_remaning_calls_to_test_server
Browse files Browse the repository at this point in the history
Add a remaining_calls helper to ApiMockTestServer
  • Loading branch information
skovmand authored Mar 21, 2019
2 parents 2a34aed + 5c303b0 commit 6c47fcc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,20 @@ defmodule MyModule.SampleTest do

alias WiseHomex.Test.ApiClientMockServer, as: MockServer

describe "ping" do
test "it calls ping with the expected includes" do
MockServer.start_link()
MockServer.set(:ping, %{query: %{"include" => "user,account"}}, {:ok, :put_mock_response_here})

config = WiseHomex.new_config(:api_key, "test")
{:ok, _response} = config |> WiseHomex.ping(%{"include" => "user,account"})
assert MockServer.called?(:ping) == %{query: %{"include" => "user,account"}}
end
test "it calls ping with the expected includes" do
config = WiseHomex.new_config(:api_key, "test")

MockServer.start_link()
MockServer.set(:ping, %{query: %{"include" => "user,account"}}, {:ok, :put_mock_response_here})

{:ok, _response} = config |> WiseHomex.ping(%{"include" => "user,account"})

# Assert on the calls that have been made
assert MockServer.called?(:ping) == %{query: %{"include" => "user,account"}}

# Assert that all set up mocks have been called
# Useful for seeing what was not called
assert MockServer.remaining_calls() == %{}
end
end
```
27 changes: 27 additions & 0 deletions lib/wise_homex/test/api_client_mock_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,23 @@ defmodule WiseHomex.Test.ApiClientMockServer do
GenServer.call(@name, {:called?, api_function})
end

@doc """
Do a mock api call and receive the set up mock value.
This will fail if no mock matches the api_function and opts.
"""
def call_and_get_mock_value(api_function, opts) do
GenServer.call(@name, {:called!, api_function, opts})
GenServer.call(@name, {:pop_mock_value, api_function, opts})
end

@doc """
Receive remaining mock calls on the Mock Server.
Useful for asserting that all calls were made.
"""
def remaining_calls() do
GenServer.call(@name, :get_all_mocks)
end

@doc """
Initialize the mock server with an empty request stack
"""
Expand Down Expand Up @@ -94,6 +106,14 @@ defmodule WiseHomex.Test.ApiClientMockServer do
[value | rest] -> {value, rest}
end)

# Clear out mock keys that refer to an empty list, which means they have been used.
state =
Map.update!(state, :mocks, fn
mocks ->
Enum.reject(mocks, fn {_k, v} -> v == [] end)
|> Enum.into(%{})
end)

return_value =
case value do
:no_mock_set -> {:error, "No mock set on #{api_function} with options #{inspect(opts)}"}
Expand All @@ -103,6 +123,13 @@ defmodule WiseHomex.Test.ApiClientMockServer do
{:reply, return_value, state}
end

@doc """
Get remaining mock calls
"""
def handle_call(:get_all_mocks, _from, state) do
{:reply, Map.fetch!(state, :mocks), state}
end

@doc """
Push a call with opts to the mock
It will update the calls map with e.g. %{get_gateways: [%{query: %{}}, %{query: %{"include" => "devices"}}, ...]}
Expand Down
38 changes: 38 additions & 0 deletions test/api_client_mock_server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ defmodule ApiClientMockServerTest do
assert MockServer.called?(:some_function) == opts
end

test "it sets and receives two mock values" do
opts = %{params: [1, 2, 3, 4], query: %{"hej" => "med_dig"}}

MockServer.set(
:some_function,
opts,
{:ok, 1234}
)

MockServer.set(
:other_function,
%{},
{:ok, 1234}
)

MockServer.call_and_get_mock_value(:some_function, opts)
MockServer.call_and_get_mock_value(:other_function, %{})

assert MockServer.called?(:some_function) == opts
assert MockServer.called?(:other_function) == %{}
end

test "it returns false if a call was not made" do
MockServer.set(
:other_function,
Expand Down Expand Up @@ -53,4 +75,20 @@ defmodule ApiClientMockServerTest do
assert MockServer.call_and_get_mock_value(:other_function, %{}) ==
{:error, "No mock set on other_function with options %{}"}
end

test "it returns remaining calls when available" do
MockServer.set(:some_function, %{}, {:ok, 1234})
MockServer.set(:other_function, %{"which" => "is not called"}, {:ok, 5678})

{:ok, 1234} = MockServer.call_and_get_mock_value(:some_function, %{})

assert MockServer.remaining_calls() == %{{:other_function, %{"which" => "is not called"}} => [ok: 5678]}
end

test "if no remaining calls are available it returns an empty list" do
MockServer.set(:some_function, %{}, {:ok, 1234})
{:ok, 1234} = MockServer.call_and_get_mock_value(:some_function, %{})

assert MockServer.remaining_calls() == %{}
end
end

0 comments on commit 6c47fcc

Please sign in to comment.