This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assert {:bad, :error} == Discord.send_message(%Message{}) | |
# We start at the Facade | |
Discord.send_message(%Message{}) { | |
# The Facade calls the Delegate | |
Delegate.send_message(message) { | |
# The Delegate checks if the Server has a patch for send_message/1 | |
Server.delegate(Discord, :send_message, [message]) { | |
# The Server doesn't, so it calls the Original | |
Original.send_message(message) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/src/discord > mix test | |
... | |
3 tests, 0 failures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Discord.Test do | |
use ExUnit.Case | |
use Patch | |
describe "send_message/1" do | |
test "errors on invalid messages" do | |
patch(Discord, :validate_message, {:error, :bad}) | |
assert {:error, :bad} == Discord.send_message(%Message{}) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Patch.Mock.Original.For.Discord do | |
alias Patch.Mock.Delegate.For.Discord, as: Delegate | |
def send_message(%Message{} = message) do | |
with :ok <- Delegate.validate_message(message), | |
:ok <- Delegate.save_message(message) do | |
Delegate.broadcast_message(message) | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def delegate(module, name, arguments) do | |
server = Naming.server(module) | |
case GenServer.call(server, {:delegate, name, arguments}) do | |
{:ok, reply} -> | |
reply | |
:error -> | |
original = Naming.original(module) | |
apply(original, name, arguments) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Patch.Mock.Delegate.For.Discord do | |
alias Patch.Mock.Server | |
def send_message(%Message{} = message) do | |
Server.delegate(Discord, :send_message, [message]) | |
end | |
def validate_message(message) do | |
Server.delegate(Discord, :validate_message, [message]) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Discord do | |
alias Patch.Mock.Delegate.For.Discord, as: Delegate | |
def send_message(%Message{} = message) do | |
Delegate.send_message(message) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test "something" do | |
patch(ModuleA, :function_a, :ok) | |
patch(ModuleA, :function_b, fn arg -> {:ok, arg} end) | |
patch(ModuleB, :function_c, false) | |
patch(ModuleB, :function_d, true) | |
assert Something.here() | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test "something" do | |
with_mocks([ | |
{ | |
ModuleA, | |
[:passthrough], | |
function_a: fn _ -> :ok end, | |
function_b: fn arg -> | |
{:ok, arg} | |
end | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/src/discord > mix test | |
1) test send_message/1 errors on invalid messages (Discord.Test) | |
test/discord_test.exs:7 | |
Assertion with == failed | |
code: assert {:error, :bad} == Discord.send_message(%Message{}) | |
left: {:error, :bad} | |
right: :ok | |
stacktrace: | |
test/discord_test.exs:9: (test) |
NewerOlder