All Projects → archan937 → Mecks_unit

archan937 / Mecks_unit

A simple Elixir package to elegantly mock module functions within (asynchronous) ExUnit tests using Erlang's :meck library

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to Mecks unit

Netty Websocket Spring Boot Starter
🚀 lightweight high-performance WebSocket framework ( 轻量级、高性能的WebSocket框架)
Stars: ✭ 885 (+1958.14%)
Mutual labels:  asynchronous
Proxay
Proxay is a record/replay proxy server that helps you write faster and more reliable tests.
Stars: ✭ 32 (-25.58%)
Mutual labels:  mocking
Gmock.py
'Google Mock' mocks generator based on libclang
Stars: ✭ 35 (-18.6%)
Mutual labels:  mocking
Faker.js
generate massive amounts of realistic fake data in Node.js and the browser
Stars: ✭ 34,329 (+79734.88%)
Mutual labels:  mocking
Python Adguardhome
Asynchronous Python client for the AdGuard Home API
Stars: ✭ 29 (-32.56%)
Mutual labels:  asynchronous
Csp
Communicating Sequential Processes in JavaScript
Stars: ✭ 33 (-23.26%)
Mutual labels:  asynchronous
Request.swift
A tiny HTTP client written in swift. URLSession alternative
Stars: ✭ 14 (-67.44%)
Mutual labels:  asynchronous
Jazzon
Add some jazz to your JSON files
Stars: ✭ 38 (-11.63%)
Mutual labels:  mocking
Tokio File Unix
Asynchronous support for epollable files via Tokio on Unix-like platforms
Stars: ✭ 29 (-32.56%)
Mutual labels:  asynchronous
Dkregistry Rs
A pure-Rust asynchronous library for Docker Registry API v2
Stars: ✭ 35 (-18.6%)
Mutual labels:  asynchronous
Tk Listen
A library that allows to listen network sockets with proper resource limits and error handling
Stars: ✭ 27 (-37.21%)
Mutual labels:  asynchronous
Moq
Interface mocking tool for go generate
Stars: ✭ 941 (+2088.37%)
Mutual labels:  mocking
Tarant
Reactive, actor based framework that can be used in client and server side.
Stars: ✭ 33 (-23.26%)
Mutual labels:  asynchronous
Iguazu Rest
✨ Iguazu REST is a plugin for the Iguazu ecosystem that allows for pre-built async calls for REST with smart caching.
Stars: ✭ 21 (-51.16%)
Mutual labels:  asynchronous
Handle Path Oz
Android Library to handle multiple Uri's(paths) received through Intents.
Stars: ✭ 36 (-16.28%)
Mutual labels:  asynchronous
Gapbot
Gap Messenger Api Bot Library for Python
Stars: ✭ 15 (-65.12%)
Mutual labels:  elegant
Queuer
Queuer is a queue manager, built on top of OperationQueue and Dispatch (aka GCD).
Stars: ✭ 964 (+2141.86%)
Mutual labels:  asynchronous
Partial.lenses.validation
Partial Lenses Validation is a JavaScript library for validating and transforming data
Stars: ✭ 39 (-9.3%)
Mutual labels:  asynchronous
Wiremockui
Wiremock UI - Tool for creating mock servers, proxies servers and proxies servers with the option to save the data traffic from an existing API or Site.
Stars: ✭ 38 (-11.63%)
Mutual labels:  mocking
Jdonframework
Domain-Driven-Design Pub/Sub Domain-Events framework
Stars: ✭ 978 (+2174.42%)
Mutual labels:  asynchronous

MecksUnit Build Status

A simple Elixir package to elegantly mock module functions within (asynchronous) ExUnit tests using Erlang's :meck library

Installation

To install MecksUnit, please do the following:

  1. Add mecks_unit to your list of dependencies in mix.exs:

    def deps do
      [
        {:mecks_unit, "~> 0.1.9", only: :test}
      ]
    end
    

Usage

Mocking module functions is pretty straightforward and done as follows:

  1. Add use MecksUnit.Case at the beginning of your test file
  2. Use defmock as if you would define the original module with defmodule containing mocked functions
  3. Use mocked_test as if you would define a normal ExUnit test after having defined all the required mock modules
  4. Add MecksUnit.mock() in your test/test_helper.exs file
  5. Enjoy ;)

Please note that the defined mock modules only apply to the first mocked_test encountered. So they are isolated (despite of :meck having an unfortunate global effect) as MecksUnit takes care of it. Also, non-matching function heads within the mock module will result in invoking the original module function as well.

As of version 0.1.2, you can assert function calls using called (returns a boolean) or assert_called (raises an error when not having found a match) within your test block. Use _ to match any argument as if you would pattern match.

Prior to version 0.1.3, you would very often get :meck related compile errors when using MecksUnit in multiple test files. From that version on, this problem is solved. Happy testing! ^^

Define mock module for entire test case

As of version 0.1.8, you can "preserve" a mocked module definition for the rest of the test case by adding preserve: true.

defmock List, preserve: true do
  def wrap(:foo), do: [1, 2, 3, 4]
end

This behaviour is intended to be implemented as natural as possible. Therefore, you can override a preserved mock module once just by inserting a "regular" mock module definition:

defmock List, preserve: true do
  def wrap(:foo), do: [1, 2, 3, 4]
end

# mocked tests ...

defmock List do
  def wrap(:foo), do: ["this only applies to the next `mocked_test`"]
end

Also, you can override a preserved mock module for the rest of the test case by using preserve: true again.

defmock List, preserve: true do
  def wrap(:foo), do: [1, 2, 3, 4]
end

# mocked tests ...

defmock List do
  def wrap(:foo), do: ["this only applies to the next `mocked_test`"]
end

# mocked tests ...

defmock List, preserve: true do
  def wrap(:foo), do: [5, 6, 7, 8]
end

Please note that this behaviour is also tested in test/mecks_unit/preserve_test.exs.

A full example

The following is a working example defined in test/mecks_unit_test.exs

# (in test/test_helper.exs)

ExUnit.start()
MecksUnit.mock()
# (in test/mecks_unit_test.exs)

defmodule Foo do
  def trim(string) do
    String.trim(string)
  end
end

defmodule MecksUnitTest do
  use ExUnit.Case, async: true
  use MecksUnit.Case

  defmock String do
    def trim("  Paul  "), do: "Engel"
    def trim("  Foo  ", "!"), do: "Bar"
    def trim(_, "!"), do: {:passthrough, ["  Surprise!  !!!!", "!"]}
    def trim(_, _), do: :passthrough
  end

  defmock List do
    def wrap(:foo), do: [1, 2, 3, 4]
  end

  mocked_test "using mocked module functions" do
    task =
      Task.async(fn ->
        assert "Engel" == String.trim("  Paul  ")
        assert "Engel" == Foo.trim("  Paul  ")
        assert "Bar" == String.trim("  Foo  ", "!")
        assert "  Surprise!  " == String.trim("  Paul  ", "!")
        assert "MecksUnit" == String.trim("  MecksUnit  ")
        assert "Paul Engel" == String.trim("  Paul Engel  ", " ")
        assert [1, 2, 3, 4] == List.wrap(:foo)
        assert [] == List.wrap(nil)
        assert [:bar] == List.wrap(:bar)
        assert [:foo, :bar] == List.wrap([:foo, :bar])
        assert called List.wrap(:foo)
        assert_called String.trim(_)
      end)

    Task.await(task)
  end

  test "using the original module functions" do
    task =
      Task.async(fn ->
        assert "Paul" == String.trim("  Paul  ")
        assert "Paul" == Foo.trim("  Paul  ")
        assert "  Foo  " == String.trim("  Foo  ", "!")
        assert "  Paul  " == String.trim("  Paul  ", "!")
        assert "MecksUnit" == String.trim("  MecksUnit  ")
        assert "Paul Engel" == String.trim("  Paul Engel  ", " ")
        assert [:foo] == List.wrap(:foo)
        assert [] == List.wrap(nil)
        assert [:bar] == List.wrap(:bar)
        assert [:foo, :bar] == List.wrap([:foo, :bar])
      end)

    Task.await(task)
  end
end

Please note that you can delegate to the original implementation by either returning :passthrough (which forwards the given arguments) or return a tuple {:passthrough, arguments} in which you can alter the arguments yourself.

Asynchronous testing

Unlike Mock, MecksUnit supports running mocked tests asynchronously. W00t! ^^

License

Copyright (c) 2020 Paul Engel, released under the MIT License

http://github.com/archan937http://twitter.com/archan937[email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].