ITADN

Problem mocking function with default arguments

#9Closedmfeckie 创建于 2025-06-12
M
mfeckiecommented
Given the following ```elixir @spec with_defaults(String.t(), list()) :: any() defeffect with_defaults(_string, _options \\ []) do end ``` for a test that looks like this: ```elixir test "on multi arity functions" do bind(EfxExample, :with_defaults, fn _string, _options -> :default end) bind(EfxExample, :with_defaults, fn _string -> :default end) assert EfxExample.with_defaults("test") == :default assert EfxExample.with_defaults("test", []) == :default end ``` we get the error ``` warning: EfxCase.EfxExample.with_defaults/1 is undefined or private. Did you mean: * with_defaults/2 │ 56 │ assert EfxExample.with_defaults("test") == :default │ ~ │ ``` I guess this is because of the way default arguments are handled by the spec, because we can't specify an optional in `@spec` If I then change and explicitly define the both arity versions ```elixir @spec with_defaults(String.t()) :: any() defeffect with_defaults(string) do with_defaults(string, []) end @spec with_defaults(String.t(), list()) :: any() defeffect with_defaults(_string, _options \\ []) do end ``` I then get ``` 17:02:19.199 [error] GenServer EfxCase.MockState terminating ** (ExUnit.AssertionError) Not matching function found for Elixir.EfxCase.EfxExample: with_defaults/1 ``` So it seems like somewhere in Efx or EfxCase there's something overwriting the implementation of `with_defaults/1`
关闭于 2025-07-30 3 条评论