Skip to content

Instantly share code, notes, and snippets.

View ihumanable's full-sized avatar

Matt Nowack ihumanable

  • San Francisco, CA
View GitHub Profile
1 2831
1 1949
1 1871
1 1550
1 1530
1 1451
1 1424
1 1420
1 1409
1 1363
Superseded by https://github.com/ihumanable/patch
@ihumanable
ihumanable / refactored.ex
Last active June 5, 2020 22:41
Refactored Twilio Processing Code
defmodule Twilio.Helper do
defmodule PhoneNumber do
alias Twilio.Helper.{CallerInfo, CarrierInfo}
defstruct [:carrier_type,
:carrier_name,
:country_code,
:national_format,
:caller_name,
:caller_type,
@ihumanable
ihumanable / invalid-message-test.exs
Created November 23, 2021 00:26
Ideal Invalid Message Test
test "errors on invalid messages" do
make_validate_message_return({:error, :bad})
assert {:error, :bad} == Discord.send_message(%Message{})
end
@ihumanable
ihumanable / discord_test.exs
Last active December 2, 2021 23:33
Discord Test with Mock: Attempt 1
defmodule Discord.Test do
use ExUnit.Case
import Mock
describe "send_message/1" do
test "errors on invalid messages" do
with_mock Discord, [validate_message: fn _ -> {:error, :bad} end] do
assert {:error, :bad} == Discord.send_message(%Message{})
end
end
@ihumanable
ihumanable / test_output_mock_1.ex
Created November 23, 2021 00:28
Test Output (Mock 1)
~/src/discord > mix test
1) test send_message/1 errors on invalid messages (Discord.Test)
test/discord_test.exs:7
** (ErlangError) Erlang error: {:undefined_function, {Discord, :validate_message, 1}}
code: with_mock Discord, [validate_message: fn(_message) -> {:error, :bad} end] do
3 tests, 1 failure
@ihumanable
ihumanable / test_output_mock_2.ex
Created November 23, 2021 00:30
Test Output (Mock 2)
~/src/discord > mix test
1) test send_message/1 errors on invalid messages (Discord.Test)
test/discord_test.exs:7
** (UndefinedFunctionError) function Discord.send_message/1 is undefined
(module Discord is not available)
code: assert {:error, :bad} == Discord.send_message(%Message{})
3 tests, 1 failure
@ihumanable
ihumanable / discord_test.exs
Created November 23, 2021 00:31
Discord Test with Mock: Attempt 2 (Added :passthrough)
defmodule Discord.Test do
use ExUnit.Case
import Mock
describe "send_message/1" do
test "errors on invalid messages" do
with_mock Discord, [:passthrough], [validate_message: fn _ -> {:error, :bad} end] do
assert {:error, :bad} == Discord.send_message(%Message{})
end
@ihumanable
ihumanable / test_output_mock_3.ex
Created November 23, 2021 00:32
Test Output (Mock 3)
~/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)
@ihumanable
ihumanable / mock_example.exs
Created November 23, 2021 00:33
Multiple Mocks Example
test "something" do
with_mocks([
{
ModuleA,
[:passthrough],
function_a: fn _ -> :ok end,
function_b: fn arg ->
{:ok, arg}
end
},