Git Product home page Git Product logo

elixir_mock's People

Contributors

joshrieken avatar wanderanimrod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

elixir_mock's Issues

defmock_of doesn't work without importing Mockex

`
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

Add "was called with any number of arguments" verification

Currently,

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())

Suggestion

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"

Pattern matching in stubbed functions does not record call args correctly

Current behaviour

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.

Inject context into mock definitions

Mock definitions do not allow context from the test defining the mocks

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

Proposal

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

Private functions on mock modules not accessible by public functions

code

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

raises

  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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.