Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

NotificationsYou must be signed in to change notification settings

archan937/mecks_unit

Repository files navigation

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 inmix.exs:

    defdepsdo[{:mecks_unit,"~> 0.1.9",only::test}]end

Usage

Mocking module functions is pretty straightforward and done as follows:

  1. Adduse MecksUnit.Case at the beginning of your test file
  2. Usedefmock as if you would define the original module withdefmodule containing mocked functions
  3. Usemocked_test as if you would define a normal ExUnittest after having defined all the required mock modules
  4. AddMecksUnit.mock() in yourtest/test_helper.exs file
  5. Enjoy ;)

Please note that the defined mock modules only apply to the firstmocked_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 version0.1.2, you can assert function calls usingcalled (returns a boolean) orassert_called (raises anerror when not having found a match) within your test block. Use_ to match any argument as if you would pattern match.

Prior to version0.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 version0.1.8, you can "preserve" a mocked module definition for the rest of the test case by addingpreserve: true.

defmockList,preserve:truedodefwrap(: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 oncejust by inserting a "regular" mock module definition:

defmockList,preserve:truedodefwrap(:foo),do:[1,2,3,4]end# mocked tests ...defmockListdodefwrap(: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 usingpreserve: true again.

defmockList,preserve:truedodefwrap(:foo),do:[1,2,3,4]end# mocked tests ...defmockListdodefwrap(:foo),do:["this only applies to the next `mocked_test`"]end# mocked tests ...defmockList,preserve:truedodefwrap(:foo),do:[5,6,7,8]end

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

A full example

The following is a working example defined intest/mecks_unit_test.exs

# (in test/test_helper.exs)ExUnit.start()MecksUnit.mock()
# (in test/mecks_unit_test.exs)defmoduleFoododeftrim(string)doString.trim(string)endenddefmoduleMecksUnitTestdouseExUnit.Case,async:trueuseMecksUnit.CasedefmockStringdodeftrim("  Paul  "),do:"Engel"deftrim("  Foo  ","!"),do:"Bar"deftrim(_,"!"),do:{:passthrough,["  Surprise!  !!!!","!"]}deftrim(_,_),do::passthroughenddefmockListdodefwrap(:foo),do:[1,2,3,4]endmocked_test"using mocked module functions"dotask=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])assertcalledList.wrap(:foo)assert_calledString.trim(_)end)Task.await(task)endtest"using the original module functions"dotask=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)endend

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

UnlikeMock, MecksUnit supports running mocked tests asynchronously. W00t! ^^

License

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

http://github.com/archan937http://twitter.com/archan937pm_engel@icloud.com

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.

About

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

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp