Code Monkey home page Code Monkey logo

Comments (6)

ksakhamuri avatar ksakhamuri commented on August 26, 2024

I'm running into the same issue. I think, it's a common case for a supporting library to throw exceptions and for us to handle the exceptions accordingly. And I sure would love the ability to test for that path too.

Referring to the documentation of Meck, there seems to be a provision for "expecting" exceptions.

Exceptions can be anticipated by Meck (resulting in validation still passing). This is intended to be used to test code that can and should handle certain exceptions indeed does take care of them:

5> meck:expect(dog, meow, fun() -> meck:exception(error, not_a_cat) end).
ok
6> catch dog:meow().
{'EXIT',{not_a_cat,[{meck,exception,2},
                    {meck,exec,4},
                    {dog,meow,[]},
                    {erl_eval,do_apply,5},
                    {erl_eval,expr,5},
                    {shell,exprs,6},
                    {shell,eval_exprs,6},
                    {shell,eval_loop,3}]}}
7> meck:validate(dog).
true

Is this something that we can support in Mock? I'm new to Elixir (and totally unfamiliar with Erlang), and haven't read up on macros. Otherwise, would love to contribute back.

from mock.

emilsoman avatar emilsoman commented on August 26, 2024

Btw, I stopped using mock in favor of dependency injection.

from mock.

ksakhamuri avatar ksakhamuri commented on August 26, 2024

That's good to know @emilsoman. Is there a different library you are using to achieve it? Or can you point me at a place where I can understand how to do this in elixir?

Thanks,
Kalyan

from mock.

emilsoman avatar emilsoman commented on August 26, 2024

Here's a quick demo of how I changed my code for DI :

#Change this ->
defmodule MyModule do
  def get_project(url, dir) do
    Git.clone(url, dir)
  end
end


# to this ->
# See how we use a default argument.
# This means the consumers of this function don't
# have to know this change and everything works!
defmodule MyModule do
  def get_project(url, dir, scm \\ Git) do
    scm.clone(url, dir)
  end
end

Now you can test it like this:

test "get_project clones a repo using scm" do
  defmodule FakeGit do
    def clone(url, dir) do
      send self, {url, dir}
    end
  end

  MyModule.get_project("fake_url", "fake_dir", FakeGit)

  receive do
    {url, dir} ->
      assert url == "fake_url"
      assert dir == "fake_dir"
  end
end

There are other ways to do this. Check this blog post. But I find DI using function arguments the best way to do this, YMMV.

from mock.

ksakhamuri avatar ksakhamuri commented on August 26, 2024

Thanks Emil for those pointers. Very helpful!

from mock.

Olshansk avatar Olshansk commented on August 26, 2024

Going to close out this issue.

Simply for the record, I did want to point out that erlang code can be executed from within elixir. For example: meck:expect(..) can be written as `:meck.expect(...).

from mock.

Related Issues (20)

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.