wanderanimrod / elixir_mock Goto Github PK
View Code? Open in Web Editor NEWCreates clean, concurrent, inspectable mocks from elixir modules
Home Page: https://hexdocs.pm/elixir_mock/
License: MIT License
Creates clean, concurrent, inspectable mocks from elixir modules
Home Page: https://hexdocs.pm/elixir_mock/
License: MIT License
with_mock(mock_elastic) = defmock_of Tirexs.HTTP do
def get("/sensor_messages/_search" <> anything_else) do
Store.EventsRepoTest.sensor_events_in_db() |> to_es_response
end
defp to_es_response(events) do
{:ok, 200, %{hits: %{
hits: events,
total: length(events)}}
}
end
end
1) test should find sensor messages by sensor id (Store.EventsRepoTest)
test/lib/events_repo_test.exs:23
** (CompileError) test/lib/events_repo_test.exs:10: undefined function to_es_response/1
stacktrace:
(stdlib) lists.erl:1338: :lists.foreach/2
(elixir) src/elixir_module.erl:97: :elixir_module.do_compile/5
(elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/3
test/lib/events_repo_test.exs:8: Store.EventsRepoTest.__ex_unit_setup_0/1
test/lib/events_repo_test.exs:1: Store.EventsRepoTest.__ex_unit__/2
Since mocks are not allowed to define public functions not defined on the original modules, making the to_es_response
function public is not a feasible workaround
if I want to verify that a function with arity 3 was called but I don't care about the number of args, I still have to say
import Mockex.Matchers
assert_called mock.function(any(), any(), any())
import Mockex.Matchers
assert_called mock.function, :any_args
# OR for consistency with other matchers
assert_called mock.function(any_number_of_args())
We can't simply use assert_called mock.function
because it means "assert called with no args"
Create two tests with a setup method that creates a mock module and passes the module to the tests. The tests will raise a module redefinition
warning. Find out why this happens.
Tested using the defmock_of
macro
`
defmodule MyTest do
use ExUnit.Case
require Mockex
test "fails to compile" do
with_mock(mock) = defmock_of GenServer do
def start, do: {:ok, self()}
end
end
end
`
This will fail to compile with error cannot invoke def/2 inside function/macro
Is this even possible?
All within an exunit test context
with_mock(mock) = defmock_of RealModule do
def is_age_one?(%{age: 1}) do
{:ok, "age is one"}
end
def is_age_one?(%{age: age}) do
{:error, "age is not one. it is #{age}"}
end
end
response = mock.is_age_one? %{age: 1, height: 1.74}
assert response == {:ok, "age is one"} #should pass
assert_called mock.is_age_one?(%{age: 1, height: 1.74})
# this fails, only recording the section of the arg in the
# function's pattern match which is: %{age: 1}
This could also imply that pattern matching with functions in mock definitions does not work correctly, particularly with maps, and possibly with other data structures.
test "my test" do
my_var = 10
with_mock(my_mock) = defmock_of RealModule do
def stubbed, do: my_var
end
my_mock.stubbed() # Will raise 'undefined function my_var'
end
test "my test" do
my_var = 10
with_mock(my_mock) = defmock_of RealModule, %{injected_var: my_var} do
def stubbed, do: mockex_context(:injected_var)
end
my_mock.stubbed() # => 10
end
refute_called mock.some_fn(Mockex.Matchers.any())
Did not expect get({:matches, #Function<0.115935979/1 in Mockex.Matchers.InBuilt.any/1>}) to be called but it was.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.