Coder Social home page Coder Social logo

archan937 / mecks_unit Goto Github PK

View Code? Open in Web Editor NEW
55.0 7.0 7.0 45 KB

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

Home Page: http://elixirstatus.com/p/iXWw-mecksunit-elegantly-mock-module-functions-in-async-exunit-tests

Elixir 100.00%
elixir mocking exunit meck asynchronous elegant

mecks_unit's Issues

Module attributes are not visible inside defmock block

Hello @archan937, thank you very much for this nice tool!

Using module attribute inside defmock gives a compilation error.
In test module:

@uuid UUID.generate()

defmock SomeApp.Repo do
    def get(SomeApp.Schema.Application, @uuid), do: %SomeApp.Schema.Application{}
end

In console:

> mix test

warning: undefined module attribute @uuid, please remove access to @uuid or explicitly set it before access
  nofile:106: SomeApp.RepoTest0.SomeApp.Repo (module)

Just one mock with "preserve" doesn't work

Hi all,

first of all, thanks for this great hex, it's looking really awesome! :-)

We noticed that the new preserve: true feature doesn't work if there's just one Mock defined in the test, for example:

defmodule Foo
  def bar(x), do: x
end

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

  # defmock Foo do
  #   def bar(_x), do: 1
  # end

  defmock Foo, preserve: true do
    def bar(_x), do: 1
  end

  mocked_test "this should work" do
    assert Foo.bar(nil) == 1
  end

  mocked_test "this should work as well" do
    assert Foo.bar(nil) == 1
  end
end
mix test test/foo_test.exs 

FooTest
  * test this should work as well (2.9ms)

  1) test this should work as well (FooTest)
     test/foo_test.exs:17
     Assertion with == failed
     code:  assert Foo.bar(nil) == 1
     left:  nil
     right: 1
     stacktrace:
       test/foo_test.exs:18: (test)

  * test this should work (0.02ms)

  2) test this should work (FooTest)
     test/foo_test.exs:13
     Assertion with == failed
     code:  assert Foo.bar(nil) == 1
     left:  nil
     right: 1
     stacktrace:
       test/foo_test.exs:14: (test)



Finished in 0.04 seconds
2 tests, 2 failures

Randomized with seed 711881

By activating the first mock, the second one actually starts working.

Problem with mockings

Hey!
I want to use this library though I am not entirely sure it fit my purpose.
I have created the below modules to show you my case:

defmodule Mocked do
  def func() do
    Mocked.ModuleA.call_module_b_func()
  end

  def func2() do
    Mocked.ModuleB.module_a_func()
  end
end
defmodule Mocked.ModuleA do
  alias Mocked.ModuleB

  def module_a_func() do
    :returns_from_module_a
  end

  def call_module_b_func() do
    ModuleB.module_b_func()
  end
end 

defmodule Mocked.ModuleB do
  alias Mocked.ModuleA

  def module_b_func() do
    :returns_from_module_b
  end

  def module_a_func() do
    ModuleA.module_a_func()
  end
end

and I try to run the following test which fails if I run both tests but succeeds when I run them separately.

defmodule MockedTest do
  use ExUnit.Case
  use MecksUnit.Case

  defmock Mocked.ModuleB do
    def module_b_func() do
      :returns_from_mocking
    end
  end

  defmock Mocked.ModuleA do
    def module_a_func() do
      :returns_from_mocking
    end
  end

  mocked_test "mocking works" do
    assert Mocked.func() === :returns_from_mocking
  end

  mocked_test "mocking works again" do
    assert Mocked.func2() === :returns_from_mocking
  end
end

Do you have any insights on this problem?

Thanks! ๐Ÿ™‚

Mocks are not found when running "mix test" with non-path arguments

Run tests with

mix test --trace

In that case, mix runs all the tests with more details. MecksUnit checks System.argv() and if there are any arguments, they are treated like path wildcards. Files resulting from that wildcards expansion are scanned for mocks. However, in the above case, the --trace argument is clearly not a file path, so no files are scanned for mocks, but all tests will run.

Possible fix ideas:

  • When the wildcards expansion results in empty list, fall back to the default wildcard pattern "test/**/*.exs". This still won't fix situations like mix test --exclude test/my_test.exs. In that case, all tests except for test/my_test.exs will run, but mocks will be looked for only in the test/my_test.exs.
  • Dig into ExUnit and find out if it's possible to hook into it to get the list of tests to be run before they actually run.

What do you think?

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.