Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with_mocks/3 is undefined #80

Open
chingan-tsc opened this issue Dec 7, 2017 · 8 comments
Open

with_mocks/3 is undefined #80

chingan-tsc opened this issue Dec 7, 2017 · 8 comments

Comments

@chingan-tsc
Copy link

Hi I was trying to use with_mocks/3

with_mocks(HTTPoison, [
      get: fn
        "http://example.com" -> "<html>Hello from example.com</html>"
        "http://example.org" -> "<html>example.org says hi</html>"
      end
    ]) do

like written in the docs but it throws an undefined function error. Any ideas why?

@Olshansk
Copy link
Collaborator

Olshansk commented Dec 7, 2017

You'd need to provide a little more context. Which function is getting called when an undefined error is thrown?

Make you need to use the :passthrough parameter? Perhaps your get fn is receiving some sort of input you do not account for and you should consider adding an _ case to catch all the other inputs?

@chingan-tsc
Copy link
Author

chingan-tsc commented Dec 7, 2017

It throws an undefined function with_mocks/3 error when I tried to run this block

test "mock functions with multiple returns" do
    with_mocks(HTTPotion, [
      get: fn
        "http://example.com" -> "<html>Hello from example.com</html>"
        "http://example.org" -> "<html>example.org says hi</html>"
      end
    ]) do
      assert HTTPotion.get("http://example.com") == "<html>Hello from example.com</html>"
      assert HTTPotion.get("http://example.org") == "<html>example.org says hi</html>"
    end
  end

I got from the documentation without changing anything.

@Olshansk
Copy link
Collaborator

Olshansk commented Dec 7, 2017

Did you import Mock?

@chingan-tsc
Copy link
Author

Yes I did, I have other with_mocks in the same test suite. For one particular test that involves multiple HTTPotion calls I need the capability of "different returns based on the input" and I saw that block in the docs but it just didn't work for me.

@chingan-tsc
Copy link
Author

After some digging on the codebase, I fixed it by using with_mock instead of with_mocks. It works well now but I think the documentation is confusing at times.

For instance, I have also tried using the method below as well:

with_mock String,
      [slice: fn(string, range)      -> string end,
       slice: fn(string, range, len) -> string end]
    do
      assert String.slice("test", 1..3) == "test"
      assert String.slice("test", 1, 3) == "test"
    end

And I realize it only works if in the test case we are calling String.slice/2 and String.slice/3 (multiple arities) regardless on the parameter value (i.e it won't do param pattern matching AFAIK).

On the other hand, if we do:

with_mock(HTTPotion, [
      get: fn
        "http://example.com" -> "<html>Hello from example.com</html>"
        "http://example.org" -> "<html>example.org says hi</html>"
      end
    ]) do
      assert HTTPotion.get("http://example.com") == "<html>Hello from example.com</html>"
      assert HTTPotion.get("http://example.org") == "<html>example.org says hi</html>"
    end

it will do param pattern matching. Note that syntactically they are lil bit different (placement of end) IMHO we don't need the earlier method, it could be replaced by:

with_mock String,
      [slice: fn
        string, _ -> string
        string, _, _ -> string
    ] do
      assert String.slice("test", 1..3) == "test"
      assert String.slice("test", 1, 3) == "test"
    end

Please do let me know what do you think.

@Olshansk
Copy link
Collaborator

@chingan-tsc Do you think a change to the documentation will be sufficient?

@Cyan101
Copy link

Cyan101 commented Jul 12, 2019

documentation and the readme haven't been updated to show with_mock instead of with_mocks yet for this particular case

@Olshansk
Copy link
Collaborator

I could update it myself but PRs are more than welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants