Skip to content

Commit

Permalink
add mix task for tests, mocks and integration
Browse files Browse the repository at this point in the history
Added mix task to generate test file for the gateways, add
a file to define the mock responses and also to generate
integration test.
  • Loading branch information
arjun289 authored and pkrawat1 committed Jan 25, 2018
1 parent f3137a3 commit fe58149
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/mix/new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ Comma separated list of required configuration keys:
gateway_module: module_name,
gateway_underscore: Macro.underscore(name),
required_config_keys: required_keys,
gateway_url: url
gateway_url: url,
gateway_mock_test: Macro.underscore(name) <> "_test",
gateway_mock_response: Macro.underscore(name) <> "_mock",
]

if (Mix.Shell.IO.yes? "\nDoes this look good?\n#{inspect(bindings, pretty: true)}\n>") do
gateway = EEx.eval_file("templates/gateway.eex", bindings)
# mock = ""
# integration = ""
mock = EEx.eval_file("templates/test.eex", bindings)
mock_response = EEx.eval_file("templates/mock_response.eex", bindings)
integration = EEx.eval_file("templates/integration.eex", bindings)

create_file("lib/gringotts/gateways/#{bindings[:gateway_underscore]}.ex", gateway)
create_file("test/gateways/#{bindings[:gateway_mock_test]}.exs", mock)
create_file("test/mocks/#{bindings[:gateway_mock_response]}.exs", mock_response)
create_file("test/integration/gateways/#{bindings[:gateway_mock_test]}.exs", integration)
else
Mix.Shell.IO.info("Doing nothing, bye!")
end
Expand Down
36 changes: 36 additions & 0 deletions templates/integration.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Gringotts.Integration.Gateways.<%= gateway_module <> "Test"%> do
# Integration tests for the <%= gateway_module%>

use ExUnit.Case, async: false
alias Gringotts.Gateways.<%= gateway_module%>

@moduletag :integration

setup_all do
Application.put_env(:gringotts, Gringotts.Gateways.<%= gateway_module%>,
[
adapter: Gringotts.Gateways.<%= gateway_module%><%= if required_config_keys != [] do %><%= for key <- Enum.intersperse(required_config_keys, ",") do %><%= if key === "," do %><%= "#{key}" %><%= else %>
<%= "#{key}" %>: "your_secret_<%= "#{key}" %>"<% end %><% end %><% end %>
]
)
end

# Group the test cases by public api
describe "purchase" do
end

describe "authorize" do
end

describe "capture" do
end

describe "void" do
end

describe "refund" do
end

describe "environment setup" do
end
end
9 changes: 9 additions & 0 deletions templates/mock_response.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Gringotts.Gateways.<%= gateway_module <> "Mock"%> do

# The module should include mock responses for test cases in <%= gateway_mock_test <> ".exs"%>.
# e.g.
# def successful_purchase do
# {:ok, %HTTPoison.Response{body: ~s[{data: "successful_purchase"}]}
# end

end
32 changes: 32 additions & 0 deletions templates/test.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Gringotts.Gateways.<%= gateway_module <> "Test" %> do
# The file contains mocked tests for <%= gateway_module%>

# We recommend using [mock][1] for this, you can place the mock responses from
# the Gateway in `test/mocks/<%= gateway_mock_response%>.exs` file, which has also been
# generated for you.
#
# [1]: https://github.com/jjh42/mock

# Load the mock response file before running the tests.
Code.require_file "../mocks/<%= gateway_mock_response <> ".exs"%>", __DIR__

use ExUnit.Case, async: false
alias Gringotts.Gateways.<%= gateway_module%>
import Mock

# Group the test cases by public api
describe "purchase" do
end

describe "authorize" do
end

describe "capture" do
end

describe "void" do
end

describe "refund" do
end
end

0 comments on commit fe58149

Please sign in to comment.